/// <summary> /// The build exception. /// </summary> /// <param name="statusCode"> /// The status code. /// </param> /// <param name="baseErrorCode"> /// The base error code. /// </param> /// <param name="reason"> /// The reason. /// </param> /// <param name="message"> /// The message. /// </param> /// <param name="errorList"> /// The error list. /// </param> /// <returns> /// The <see cref="Exception"/>. /// </returns> protected Exception BuildException( HttpStatusCode statusCode, Enumeration <int> baseErrorCode, string reason, string message = "exception", List <string> errorList = null) { var exceptionResponse = new ExceptionMessage { Message = message, NestedMessages = errorList?.Select(e => new NestedMessage { Message = e }).ToList(), ErrorCodeValue = baseErrorCode, CorrelationId = ServiceLocator.Instance.Current.Resolve <IOperationContext>().CorrelationId }; this.Content = new ObjectContent(typeof(ExceptionMessage), exceptionResponse, new JsonMediaTypeFormatter()); return(this.Generate(statusCode, this.Content, reason)); }
/// <summary> /// The generate. /// </summary> /// <param name="httpStatusCode"> /// The http status code. /// </param> /// <param name="objectContent"> /// The object content. /// </param> /// <param name="data"></param> /// <returns> /// The <see cref="Exception"/>. /// </returns> public Exception Generate(HttpStatusCode httpStatusCode, ObjectContent objectContent, IDictionary data = null) { string appExceptionMessage = ""; if (objectContent.Value is ExceptionMessage) { ExceptionMessage exceptionMessage = objectContent.Value as ExceptionMessage; appExceptionMessage = exceptionMessage.Message; } var exception = Activator.CreateInstance(typeof(T), appExceptionMessage, "NoStackTrace", data) as T; exception.HttpStatusCode = httpStatusCode; exception.Content = objectContent; return(exception); // var exception = new AppException(appExceptionMessage, "NoStackTrace", data) { HttpStatusCode = httpStatusCode, Content = objectContent }; // return exception; }
/// <summary> /// The get exception. /// </summary> /// <param name="errorList"> /// The error list. /// </param> /// <param name="message"> /// The message. /// </param> /// <param name="isTopAsRoot"> /// The is top as root. /// </param> /// <returns> /// The <see cref="Exception"/>. /// </returns> public Exception GetException(List <ExceptionMessage> errorList, string message = "exception", bool isTopAsRoot = false) { if (errorList != null && errorList.Count > 0) { return(isTopAsRoot ? this.BuildException(HttpStatusCode.BadRequest, errorList.Count == 1 ? null : errorList.Skip(1).ToList(), errorList[0].ErrorCodeValue, errorList[0].Message) : this.BuildException(HttpStatusCode.BadRequest, errorList, BaseErrorCodes.InvalidInput, message)); } var exceptionResponse = new ExceptionMessage { Message = "No item found in exception list", NestedMessages = null, ErrorCodeValue = BaseErrorCodes.InvalidInput, }; this.Content = new ObjectContent(typeof(ExceptionMessage), exceptionResponse, new JsonMediaTypeFormatter()); return(this.Generate(HttpStatusCode.BadRequest, this.Content)); }
private void HandleException(System.Exception ex, IOwinContext context) { string exceptionMessage = ""; AppException exception = null; var logger = context.GetDependencyResolver().Resolve <ILogger>(); bool showNestedMessage = this._exceptionConfiguration.ShowNestedMessage(); if (ex is AppException) { exception = (AppException)ex; // exceptionMessage = $"An handled exception occured with StatusCode {exception.HttpStatusCode}"; if (exception.Content.Value is ExceptionMessage) { ExceptionMessage exMessage = exception.Content.Value as ExceptionMessage; var exceptionMsg = $"An AppException is initiated with HttpStatusCode: {(int)exception.HttpStatusCode}, \nOrginalAppException: {JsonConvert.SerializeObject(exMessage, Formatting.Indented, new JsonSerializerSettings { MaxDepth = 5 })}, \n OrginalException: {JsonConvert.SerializeObject(ex, Formatting.Indented, new JsonSerializerSettings { MaxDepth = 5 })}"; if (ex is Exceptions.ExternalRequestException || ex is Exceptions.ConfigException) { logger.Exception(new System.Exception(exceptionMsg)); } else { logger.Warning(() => exceptionMsg); } if (exMessage.ErrorCodeValue != BaseErrorCodes.CommandValidation) { exMessage.IsDetailExposable = showNestedMessage; } } } else { exception = (AppException) context.GetDependencyResolver() .Resolve <InternalServerErrorException>() .GetException(BaseErrorCodes.UnhandledException, ex, showNestedMessage, ex.Message); exceptionMessage = $"An exception occured with StatusCode {exception.HttpStatusCode}"; logger .Exception(new System.Exception($"{exceptionMessage}, \n OrginalException:- {JsonConvert.SerializeObject(ex, Formatting.Indented, new JsonSerializerSettings { MaxDepth = 5 })}")); } context.Response.ReasonPhrase = exception.ReasonPhrase; context.Response.StatusCode = (int)exception.HttpStatusCode; context.Response.ContentType = "application/json"; var jsonSerializerSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }; var json = JsonConvert.SerializeObject(exception.Content.Value, jsonSerializerSettings); context.Response.Write(json); }
/// <summary> /// The get exception. /// </summary> /// <param name="exceptionMessage"> /// The exception message. /// </param> /// <param name="httpStatusCode"> /// The http status code. /// </param> /// <param name="resonPhrase"> /// The reson phrase. /// </param> /// <returns> /// The <see cref="Exception"/>. /// </returns> public Exception GetException(ExceptionMessage exceptionMessage, HttpStatusCode httpStatusCode, string resonPhrase) { return(this.BuildException(httpStatusCode, exceptionMessage, resonPhrase)); }