public override void OnException(HttpActionExecutedContext context) { ErrorDTO errorDto; // Collect error messages of all inner exceptions var alertManager = ObjectFactory.GetInstance <EventReporter>(); var ex = context.Exception; //Post exception information to AppInsights Dictionary <string, string> properties = new Dictionary <string, string>(); foreach (KeyValuePair <string, object> arg in context.ActionContext.ActionArguments) { properties.Add(arg.Key, JsonConvert.SerializeObject(arg.Value)); } new TelemetryClient().TrackException(ex, properties); alertManager.UnhandledErrorCaught($"Unhandled exception has occurred.\r\nError message: {ex.GetFullExceptionMessage()}\r\nCall stack:\r\n{ex.StackTrace}"); if (ex.GetType() == typeof(HttpException)) { var httpException = (HttpException)ex; context.Response = new HttpResponseMessage((HttpStatusCode)httpException.GetHttpCode()); context.Response = context.Request.CreateResponse(HttpStatusCode.Forbidden, ErrorDTO.AuthenticationError("Authorization has been denied for this request.")); return; } if (ex.GetType() == typeof(MissingObjectException)) { var missingObjectEx = (MissingObjectException)ex; context.Response = context.Request.CreateResponse(HttpStatusCode.BadRequest, ErrorDTO.InternalError( missingObjectEx.Message, "MISSING_OBJECT")); return; } if (ex.GetType() == typeof(WrongAuthenticationTypeException)) { context.Response = context.Request.CreateResponse(HttpStatusCode.BadRequest, ErrorDTO.AuthenticationError("Terminal doesn't require authentication")); return; } context.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError); if (ex is AuthenticationExeception) { errorDto = ErrorDTO.AuthenticationError(); } else { errorDto = ErrorDTO.InternalError(); } errorDto.Message = "Sorry, an unexpected error has occurred while serving your request. Please try again in a few minutes."; // if debugging enabled send back the details of exception as well if (HttpContext.Current.IsDebuggingEnabled || string.Equals(CloudConfigurationManager.GetSetting("ForceExtendedDebuggingInfo"), "true", StringComparison.InvariantCultureIgnoreCase)) { errorDto.Details = new { exception = context.Exception }; } context.Response.Content = new StringContent(JsonConvert.SerializeObject(errorDto), Encoding.UTF8, "application/json"); }
protected override void HandleUnauthorizedRequest(HttpActionContext actionContext) { var principal = actionContext.RequestContext.Principal; if (principal != null && principal.IsInRole("Guest")) { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden, ErrorDTO.AuthenticationError("You need to register before using this functionality.", null, "GuestFail")); } else { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden, ErrorDTO.AuthenticationError("Authorization has been denied for this request.")); } }
public override void OnException(HttpActionExecutedContext context) { ErrorDTO errorDto; var alertManager = ContainerObjectFactory.Container.GetInstance <EventReporter>(); var ex = context.Exception; alertManager.UnhandledErrorCaught( String.Format("Unhandled exception has occurred.\r\nError message: {0}\r\nCall stack:\r\n{1}", ex.Message, ex.Source)); context.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError); if (ex is AuthenticationExeception) { errorDto = ErrorDTO.AuthenticationError(); } else { errorDto = ErrorDTO.InternalError(); } errorDto.Message = "Sorry, an unexpected error has occurred while serving your request. Please try again in a few minutes."; // if debugging enabled send back the details of exception as well if (HttpContext.Current.IsDebuggingEnabled) { if (ex is PluginCodedException) { var pluginEx = (PluginCodedException)ex; errorDto.Details = new { errorCode = pluginEx.ErrorCode, message = pluginEx.ErrorCode.GetEnumDescription() }; } else { errorDto.Details = new { exception = context.Exception }; } } context.Response.Content = new StringContent(JsonConvert.SerializeObject(errorDto), Encoding.UTF8, "application/json"); }