-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from FlaUI/error-handling
Improve error handling of unhandled exceptions and timeouts on loading the main window
- Loading branch information
Showing
7 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using FlaUI.WebDriver.Models; | ||
using Microsoft.AspNetCore.Diagnostics; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace FlaUI.WebDriver.Controllers | ||
{ | ||
[ApiController] | ||
[ApiExplorerSettings(IgnoreApi = true)] | ||
public class ErrorController : ControllerBase | ||
{ | ||
private readonly ILogger<ErrorController> _logger; | ||
|
||
|
||
public ErrorController(ILogger<ErrorController> logger) { | ||
_logger = logger; | ||
} | ||
|
||
[Route("/error")] | ||
public IActionResult HandleError() { | ||
var exceptionHandlerFeature = HttpContext.Features.Get<IExceptionHandlerFeature>()!; | ||
|
||
_logger.LogError(exceptionHandlerFeature.Error, "Returning WebDriver error response with error code 'unknown error'"); | ||
|
||
return new ObjectResult(new ResponseWithValue<ErrorResponse>(new ErrorResponse { | ||
ErrorCode = "unknown error", | ||
Message = exceptionHandlerFeature.Error.Message, | ||
StackTrace = exceptionHandlerFeature.Error.StackTrace ?? "" })) | ||
{ | ||
StatusCode = 500 | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using FlaUI.WebDriver.Models; | ||
|
||
namespace FlaUI.WebDriver | ||
{ | ||
public class NotFoundMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly ILogger _logger; | ||
|
||
public NotFoundMiddleware(RequestDelegate next, ILogger<NotFoundMiddleware> logger) | ||
{ | ||
_next = next; | ||
_logger = logger; | ||
} | ||
|
||
public async Task Invoke(HttpContext httpContext) | ||
{ | ||
await _next(httpContext); | ||
if (!httpContext.Response.HasStarted) | ||
{ | ||
if (httpContext.Response.StatusCode == StatusCodes.Status404NotFound) | ||
{ | ||
_logger.LogError("Unknown endpoint {Path}", SanitizeForLog(httpContext.Request.Path.ToString())); | ||
await httpContext.Response.WriteAsJsonAsync(new ResponseWithValue<ErrorResponse>(new ErrorResponse | ||
{ | ||
ErrorCode = "unknown command", | ||
Message = "Unknown command" | ||
})); | ||
} | ||
else if (httpContext.Response.StatusCode == StatusCodes.Status405MethodNotAllowed) | ||
{ | ||
_logger.LogError("Unknown method {Method} for endpoint {Path}", SanitizeForLog(httpContext.Request.Method), SanitizeForLog(httpContext.Request.Path.ToString())); | ||
await httpContext.Response.WriteAsJsonAsync(new ResponseWithValue<ErrorResponse>(new ErrorResponse | ||
{ | ||
ErrorCode = "unknown method", | ||
Message = "Unknown method for this endpoint" | ||
})); | ||
} | ||
} | ||
} | ||
|
||
private static string SanitizeForLog(string str) | ||
{ | ||
return str.Replace("\r", "").Replace("\n", ""); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters