public static (WebApiMessage message, int statusCode, bool exceptionHandled) HandleApiException(ClaimsPrincipal user, Exception exception) { bool exceptionHandled = false; int statusCode = (int)HttpStatusCode.InternalServerError; WebApiMessage messageObject = null; if (exception is UnauthorizedErrors) { var errors = (UnauthorizedErrors)exception; var errorList = new List <string>(); foreach (var validationError in errors.Errors) { errorList.Add(validationError.PropertyExceptionMessage); } messageObject = WebApiMessage.CreateWebApiMessage(Messages.Unauthorised, errorList); if (user.Identity.IsAuthenticated) { statusCode = (int)HttpStatusCode.Forbidden; } else { statusCode = (int)HttpStatusCode.Unauthorized; } exceptionHandled = true; } else if (exception is OperationCanceledException) { //.NET generally just doesn't send a response at all var errorList = new List <string>(); errorList.Add(Messages.RequestCancelled); messageObject = WebApiMessage.CreateWebApiMessage(Messages.RequestCancelled, errorList); statusCode = (int)HttpStatusCode.BadRequest; exceptionHandled = true; } else if (exception is TimeoutException) { var errorList = new List <string>(); errorList.Add(Messages.RequestTimedOut); messageObject = WebApiMessage.CreateWebApiMessage(Messages.RequestTimedOut, errorList); statusCode = (int)HttpStatusCode.GatewayTimeout; exceptionHandled = true; } else { } return(messageObject, statusCode, exceptionHandled); }
protected virtual IActionResult ApiSuccessMessage(string message, Object id, HttpStatusCode statusCode = HttpStatusCode.OK) { var errorList = new List <string>(); var response = WebApiMessage.CreateWebApiMessage(message, errorList, id); var result = new ObjectResult(response); result.StatusCode = (int)statusCode; return(result); }
protected virtual ActionResult ApiErrorMessage(string message, string errorMessage, int errorStatusCode = 400) { var errorList = new List <string>(); errorList.Add(errorMessage); var response = WebApiMessage.CreateWebApiMessage(message, errorList); var result = new ObjectResult(response); result.StatusCode = errorStatusCode; return(result); }
public static WebApiMessage CreateWebApiMessage(string message, ModelStateDictionary modelState) { if (modelState == null) { throw new ArgumentNullException(nameof(modelState)); } var errors = modelState.Values.SelectMany(v => v.Errors); var errorList = new List <string>(); foreach (var error in errors) { errorList.Add(error.ErrorMessage); } var response = WebApiMessage.CreateWebApiMessage(message, errorList, modelState); return(response); }
protected List <WebApiMessage> BulkDeleteResponse(IEnumerable <Result> results) { var webApiMessages = new List <WebApiMessage>(); foreach (var result in results) { if (result.IsSuccess) { webApiMessages.Add(WebApiMessage.CreateWebApiMessage(Messages.DeleteSuccessful, new List <string>())); } else { webApiMessages.Add((WebApiMessage)((ObjectResult)ValidationErrors(result)).Value); } } //For bulk return 200 regardless return(webApiMessages); }
public static (WebApiMessage message, int statusCode, bool exceptionHandled) HandleApiExceptionGlobal(Exception exception, bool showExceptionMessage) { bool exceptionHandled = true; int statusCode = (int)HttpStatusCode.InternalServerError; WebApiMessage messageObject = null; var errorList = new List <string>(); if (showExceptionMessage) { errorList.Add(Messages.UnknownError); } else { errorList.Add(exception.Message); } messageObject = WebApiMessage.CreateWebApiMessage(Messages.UnknownError, errorList); statusCode = (int)HttpStatusCode.InternalServerError; return(messageObject, statusCode, exceptionHandled); }
public WebApiMessage BuildWebApiMessageObject() { return(WebApiMessage.CreateWebApiMessage(Message, Errors, ModelState)); }