Skip to content

Commit

Permalink
fix http status code (microsoft#498)
Browse files Browse the repository at this point in the history
Signed-off-by: catcherwong <[email protected]>
  • Loading branch information
catcherwong committed Jul 11, 2023
1 parent a1ec3ee commit a0c1550
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public async Task<HttpResponseData> Run(
{
_logger.LogError(ex, "An unexpected error was encountered.");
var badRequest = req.CreateResponse(HttpStatusCode.BadRequest);
await badRequest.WriteAsJsonAsync(new { ex.Message });
await badRequest.WriteAsJsonAsync(new { ex.Message }, badRequest.StatusCode);
return badRequest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public async Task<HttpResponseData> Run(
{
_logger.LogError(ex, "An unexpected error was encountered.");
var badRequest = req.CreateResponse(HttpStatusCode.BadRequest);
await badRequest.WriteAsJsonAsync(new { Message = $"{ex.Message}" });
await badRequest.WriteAsJsonAsync(new { Message = $"{ex.Message}" }, badRequest.StatusCode);
return badRequest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ ExecutionContext context
if (string.IsNullOrWhiteSpace(input.Url))
{
var badResponse = req.CreateResponse(HttpStatusCode.BadRequest);
await badResponse.WriteAsJsonAsync(new { Message = "The url parameter can not be empty." });
await badResponse.WriteAsJsonAsync(new { Message = "The url parameter can not be empty." }, badResponse.StatusCode);
return badResponse;
}

// Validates if input.url is a valid aboslute url, aka is a complete refrence to the resource, ex: http(s)://google.com
if (!Uri.IsWellFormedUriString(input.Url, UriKind.Absolute))
{
var badResponse = req.CreateResponse(HttpStatusCode.BadRequest);
await badResponse.WriteAsJsonAsync(new { Message = $"{input.Url} is not a valid absolute Url. The Url parameter must start with 'http://' or 'http://'." });
await badResponse.WriteAsJsonAsync(new { Message = $"{input.Url} is not a valid absolute Url. The Url parameter must start with 'http://' or 'http://'." }, badResponse.StatusCode);
return badResponse;
}

Expand All @@ -106,7 +106,7 @@ ExecutionContext context
if (await stgHelper.IfShortUrlEntityExist(newRow))
{
var badResponse = req.CreateResponse(HttpStatusCode.Conflict);
await badResponse.WriteAsJsonAsync(new { Message = "This Short URL already exist." });
await badResponse.WriteAsJsonAsync(new { Message = "This Short URL already exist." }, badResponse.StatusCode);
return badResponse;
}
}
Expand All @@ -127,7 +127,7 @@ ExecutionContext context
_logger.LogError(ex, "An unexpected error was encountered.");

var badResponse = req.CreateResponse(HttpStatusCode.BadRequest);
await badResponse.WriteAsJsonAsync(new { ex.Message });
await badResponse.WriteAsJsonAsync(new { ex.Message }, badResponse.StatusCode);
return badResponse;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public async Task<HttpResponseData> Run(
{
_logger.LogError(ex, "An unexpected error was encountered.");
var badres = req.CreateResponse(HttpStatusCode.BadRequest);
await badres.WriteAsJsonAsync(new { ex.Message });
await badres.WriteAsJsonAsync(new { ex.Message }, badres.StatusCode);
return badres;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ ExecutionContext context
if (string.IsNullOrWhiteSpace(input.Url))
{
var badRequest = req.CreateResponse(HttpStatusCode.BadRequest);
await badRequest.WriteAsJsonAsync(new { Message = "The url parameter can not be empty." });
await badRequest.WriteAsJsonAsync(new { Message = "The url parameter can not be empty." }, badRequest.StatusCode);
return badRequest;
}

// Validates if input.url is a valid aboslute url, aka is a complete refrence to the resource, ex: http(s)://google.com
if (!Uri.IsWellFormedUriString(input.Url, UriKind.Absolute))
{
var badRequest = req.CreateResponse(HttpStatusCode.BadRequest);
await badRequest.WriteAsJsonAsync(new { Message = $"{input.Url} is not a valid absolute Url. The Url parameter must start with 'http://' or 'http://'." });
await badRequest.WriteAsJsonAsync(new { Message = $"{input.Url} is not a valid absolute Url. The Url parameter must start with 'http://' or 'http://'." }, badRequest.StatusCode);
return badRequest;
}

Expand All @@ -112,7 +112,7 @@ ExecutionContext context
_logger.LogError(ex, "An unexpected error was encountered.");

var badRequest = req.CreateResponse(HttpStatusCode.BadRequest);
await badRequest.WriteAsJsonAsync(new { ex.Message });
await badRequest.WriteAsJsonAsync(new { ex.Message }, badRequest.StatusCode);
return badRequest;
}

Expand Down

0 comments on commit a0c1550

Please sign in to comment.