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"); }
public static ActivityResponseDTO ErrorActivityResponseDTOWithErrorMessage() { var result = new ActivityResponseDTO { Type = ActivityResponse.Error.ToString() }; result.AddErrorDTO(ErrorDTO.InternalError(ErrorMessage)); return(result); }
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"); }
public override void HandleCore(ExceptionHandlerContext context) { var error = ErrorDTO.InternalError(); error.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) { error.Details = new { exception = context.Exception, catchBlock = context.CatchBlock, }; } context.Result = new ErrorResult(context.ExceptionContext.Request, error); }
public async Task <ResponseMessageDTO> ForceDiscover([FromBody] JToken discoveryRef) { if (discoveryRef == null) { Logger.Error($"A terminal has submitted the /forcediscovery request with an empty discoveryRef."); return(ErrorDTO.InternalError("A terminal has submitted the / forcediscovery request with an empty discoveryRef")); } TerminalDTO terminal; if (discoveryRef.Type == JTokenType.String) { terminal = new TerminalDTO { Endpoint = discoveryRef.Value <string>() }; } else { terminal = ((JObject)discoveryRef).ToObject <TerminalDTO>(); } var discoveryResult = await _terminalDiscovery.Discover(terminal, false); if (!discoveryResult.IsSucceed) { return(ErrorDTO.InternalError($"Failed to call /discover for endoint {terminal.Endpoint}. {discoveryResult.ErrorMessage}")); } if (discoveryResult.FailedTemplates.Count > 0) { return(new ResponseMessageDTO { Message = "Terminal was registered, but the following ActivityTemplates have failed: " + string.Join("\n", discoveryResult.FailedTemplates.Select(x => $"{x.Name} version {x.Version}")) }); } return(new ResponseMessageDTO()); }
public override void HandleCore(ExceptionHandlerContext context) { //Self-host fix if (HttpContext.Current == null) { return; } var error = ErrorDTO.InternalError(); error.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)) { error.Details = new { exception = context.Exception, catchBlock = context.CatchBlock, }; } context.Result = new ErrorResult(context.ExceptionContext.Request, error); }