protected override void OnException(ExceptionContext filterContext) { if (filterContext.ExceptionHandled) { return; } var request = filterContext.RequestContext.HttpContext.Request; if (Utils.IsAjaxCall(request)) { var code = HttpStatusCode.InternalServerError; if (filterContext.Exception is ArgumentException) { code = HttpStatusCode.BadRequest; } filterContext.Result = new HttpStatusCodeResult(code, filterContext.Exception.ToString()); } else { var unwrapperException = Utils.GetUnwrapperException(filterContext.Exception); string message = string.Format("{0}\r\n Stack Trace: {1}", unwrapperException.Message, unwrapperException.ToString()); string url = request.RawUrl; string ip = Utils.GetRequestIPAddress(request); string browser = request.Browser.Browser; string version = request.Browser.Version; var viewResult = new ViewResult() { ViewName = "~/Views/Error/Exception.cshtml", ViewData = new ViewDataDictionary() }; var model = new ExceptionModel() { Browser = browser, BrowserVersion = version, IpAddress = ip, Url = url, Message = message }; if (!Utils.IsLocalhost(request)) { this.ServiceManager.LogService.Insert(new LogEntity() { CreateTime = DateTime.Now, Message = model.ToString() }); } viewResult.ViewData.Model = model; filterContext.Result = viewResult; } filterContext.ExceptionHandled = true; }
/// <summary> /// Configures a global exception handling on for app /// </summary> /// <param name="app">app to apply global exception handling on</param> public static void UseGlobalExceptionHandler(this IApplicationBuilder app) { app.UseExceptionHandler(error => { // Globally track exceptions to return appropriate http responses and status codes error.Run(async context => { // Set up exception handler to listen for exception var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>(); var exception = exceptionHandlerFeature.Error; // Set default status code for exception is 500 (Internal Server Error) if exception does not match those traced var statusCode = (int)HttpStatusCode.InternalServerError; // Globally track resource not found exceptions (404), // Badly formatted input exception (412) when model input is invalid // And unauthorization exception (401) when user fails to meet authorization requirement if (exception is ResourceNotFoundException) { statusCode = (int)HttpStatusCode.NotFound; } else if (exception is ModelFormatException) { statusCode = (int)HttpStatusCode.PreconditionFailed; } else if (exception is ArgumentOutOfRangeException) { statusCode = (int)HttpStatusCode.BadRequest; } // On exception construct error model and specify HTTP status code content type context.Response.ContentType = "application/json"; context.Response.StatusCode = statusCode; var exceptionModel = new ExceptionModel { StatusCode = statusCode, ExceptionMessage = exception.Message, StackTrace = exception.StackTrace }; // Log explicit exception message when exception occurs to log file var logService = app.ApplicationServices.GetService(typeof(ILogService)) as ILogService; logService.LogToDatabase(exceptionModel); // Send exception model as HTTP response back to client await context.Response.WriteAsync(exceptionModel.ToString()); }); }); }
/// <summary> /// Configures a global exception handling on for app /// </summary> /// <param name="app">app to apply global exception handling on</param> public static void ConfigureExceptionHandler(this IApplicationBuilder app) { app.UseExceptionHandler(error => { // Globally track exceptions to return appropriate http responses and status codes error.Run(async context => { // Set up exception handler to listen for exception var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>(); var exception = exceptionHandlerFeature.Error; // Set default status code for exception is 500 (Internal Server Error) if exception does not match those traced var statusCode = (int)HttpStatusCode.InternalServerError; // Globally track resource not found exceptions (404) and // Badly formatted input exception (412) when model input is invalid if (exception is ResourceNotFoundException) { statusCode = (int)HttpStatusCode.NotFound; } else if (exception is ParameterFormatException) { statusCode = (int)HttpStatusCode.BadRequest; } else if (exception is AuthorizationException) { statusCode = (int)HttpStatusCode.Unauthorized; } else if (exception is InputFormatException) { statusCode = (int)HttpStatusCode.PreconditionFailed; } // Log explicit exception message when exception occurs to log file var logService = app.ApplicationServices.GetService(typeof(ILogService)) as ILogService; logService.LogToFile($"Exception: {exception.Message}\n\tStatus Code: {statusCode}\n\tStack trace:\n{exception.StackTrace}"); // On exception respond with the error model format as a HTTP response back to client context.Response.ContentType = "application/json"; context.Response.StatusCode = statusCode; var exceptionResponse = new ExceptionModel { StatusCode = statusCode, Message = exception.Message }; await context.Response.WriteAsync(exceptionResponse.ToString()); }); }); }
public static void UseGlobalExceptionHandler(this IApplicationBuilder app) { app.UseExceptionHandler( error => { error.Run(async context => { // Retrieve the exception handler feature var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>(); if (exceptionHandlerFeature != null) { var exception = exceptionHandlerFeature.Error; var statusCode = (int)HttpStatusCode.InternalServerError; context.Response.ContentType = "application/json"; if (exception is ResourceNotFoundException) { statusCode = (int)HttpStatusCode.NotFound; } else if (exception is ModelFormatException) { statusCode = (int)HttpStatusCode.PreconditionFailed; } else if (exception is ArgumentOutOfRangeException) { statusCode = (int)HttpStatusCode.BadRequest; } ExceptionModel model = new ExceptionModel { StatusCode = statusCode, ExceptionMessage = exception.Message, StackTrace = exception.StackTrace }; context.Response.StatusCode = statusCode; LogService log = new LogService(new LogRepository()); log.LogToDatabase(model); await context.Response.WriteAsync(model.ToString()); } }); } ); }
public static void UseGlobalExceptionHandler(this IApplicationBuilder app) { app.UseExceptionHandler(error => { error.Run(async context => { var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>(); if (exceptionHandlerFeature != null) { var exception = exceptionHandlerFeature.Error; var statusCode = HttpStatusCode.InternalServerError; if (exception is ResourceNotFoundException) { statusCode = HttpStatusCode.NotFound; } else if (exception is ModelFormatException) { statusCode = HttpStatusCode.PreconditionFailed; } else if (exception is ArgumentOutOfRangeException) { statusCode = HttpStatusCode.BadRequest; } context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)statusCode; var exceptionModel = new ExceptionModel { StatusCode = (int)statusCode, ExceptionMessage = exception.Message, StackTrace = exception.StackTrace }; await context.Response.WriteAsync(exceptionModel.ToString()); } }); }); }
public static void ConfigureExceptionHandler(this IApplicationBuilder app) { app.UseExceptionHandler(error => { error.Run(async context => { var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>(); var exception = exceptionHandlerFeature.Error; var statusCode = (int)HttpStatusCode.InternalServerError; // handle different exceptions if (exception is ResourceNotFoundException) { statusCode = (int)HttpStatusCode.NotFound; } else if (exception is ModelFormatException) { statusCode = (int)HttpStatusCode.PreconditionFailed; } else if (exception is ArgumentOutOfRangeException) { statusCode = (int)HttpStatusCode.BadRequest; } else if (exception is ResourceAlreadyExists) { statusCode = (int)HttpStatusCode.Conflict; } context.Response.ContentType = "application/json"; context.Response.StatusCode = statusCode; var exmodel = new ExceptionModel { StatusCode = statusCode, ExceptionMessage = exception.Message }; await context.Response.WriteAsync(exmodel.ToString()); }); }); }
// POST: api/Ocr public IHttpActionResult Post([FromBody] ImageModel image) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); if (image != null) { try { var localImage = createLocalImage(image); lock (syncLock) { tessBaseApi.Process(localImage); stopWatch.Stop(); File.Delete(localImage); var response = new OcrResponseModel() { Id = Guid.NewGuid(), RequestDuration = stopWatch.Elapsed, Text = tessBaseApi.GetUTF8Text() }; return(Ok(response)); } } catch { } } stopWatch.Stop(); var exception = new ExceptionModel() { DateCreated = DateTime.Now, ExceptionMessage = "Image object could not be deserialised or ocr process failed.", RequestDuration = stopWatch.Elapsed }; return(BadRequest(exception.ToString())); }