Exemple #1
0
        public static object CreateErrorResponse(string errorCode, string errorMessage, IEnumerable <ValidationErrorField> validationErrors)
        {
            var responseStatus = ResponseStatusUtils.CreateResponseStatus(errorCode, errorMessage, validationErrors);
            var responseDto    = CreateResponseDto(null, responseStatus);

            return(new HttpError(responseDto, HttpStatusCode.BadRequest, errorCode, errorMessage));
        }
Exemple #2
0
 public ResponseStatus ToResponseStatus()
 {
     return(Response.GetResponseStatus()
            ?? ResponseStatusUtils.CreateResponseStatus(ErrorCode, Message, null));
 }
Exemple #3
0
 public static ResponseStatus CreateResponseStatus(string errorCode, string errorMessage)
 {
     return(ResponseStatusUtils.CreateResponseStatus(errorCode, errorMessage, null));
 }
Exemple #4
0
        public static ResponseStatus CreateResponseStatus(string errorCode)
        {
            var errorMessage = errorCode.SplitCamelCase();

            return(ResponseStatusUtils.CreateResponseStatus(errorCode, errorMessage, null));
        }
Exemple #5
0
 public static ResponseStatus ToResponseStatus(this ValidationErrorResult validationResult)
 {
     return(validationResult.IsValid
         ? CreateSuccessResponse(validationResult.SuccessMessage)
         : ResponseStatusUtils.CreateResponseStatus(validationResult.ErrorCode, validationResult.ErrorMessage, validationResult.Errors));
 }
Exemple #6
0
 public static ResponseStatus ToResponseStatus(this ValidationError validationException)
 {
     return(ResponseStatusUtils.CreateResponseStatus(validationException.ErrorCode, validationException.Message, validationException.Violations));
 }
Exemple #7
0
        public static ResponseStatus CreateResponseStatus(Exception ex, object request = null, bool debugMode = false)
        {
            var e = ex.UnwrapIfSingleException();

            var responseStatus = (e is IResponseStatusConvertible customStatus
                ? customStatus.ToResponseStatus()
                : null) ?? ResponseStatusUtils.CreateResponseStatus(e.GetType().Name, e.Message);

            if (responseStatus == null)
            {
                return(null);
            }

            if (debugMode)
            {
#if !NETCORE
                if (ex is System.Web.HttpCompileException compileEx && compileEx.Results.Errors.HasErrors)
                {
                    responseStatus.Errors ??= new List <ResponseError>();
                    foreach (var err in compileEx.Results.Errors)
                    {
                        responseStatus.Errors.Add(new ResponseError {
                            Message = err.ToString()
                        });
                    }
                }
#endif
                // View stack trace in tests and on the client
                var sb = StringBuilderCache.Allocate();

                if (request != null)
                {
                    try
                    {
                        var str = $"[{request.GetType().GetOperationName()}: {DateTime.UtcNow}]:\n[REQUEST: {TypeSerializer.SerializeToString(request)}]";
                        sb.AppendLine(str);
                    }
                    catch (Exception requestEx)
                    {
                        sb.AppendLine($"[{request.GetType().GetOperationName()}: {DateTime.UtcNow}]:\n[REQUEST: {requestEx.Message}]");
                    }
                }

                sb.AppendLine(e.ToString());

                var innerMessages = new List <string>();
                var innerEx       = e.InnerException;
                while (innerEx != null)
                {
                    sb.AppendLine("");
                    sb.AppendLine(innerEx.ToString());
                    innerMessages.Add(innerEx.Message);
                    innerEx = innerEx.InnerException;
                }

                responseStatus.StackTrace = StringBuilderCache.ReturnAndFree(sb);
                if (innerMessages.Count > 0)
                {
                    responseStatus.Meta ??= new Dictionary <string, string>();
                    responseStatus.Meta["InnerMessages"] = innerMessages.Join("\n");
                }
            }

            return(responseStatus);
        }
        public virtual ResponseStatus CreateResponseStatus(Exception ex, object request = null)
        {
            var e = ((Config.ReturnsInnerException && ex.InnerException != null && !(ex is IHttpError)
                ? ex.InnerException
                : null) ?? ex).UnwrapIfSingleException();

            var responseStatus = (e is IResponseStatusConvertible customStatus
                ? customStatus.ToResponseStatus()
                : null) ?? ResponseStatusUtils.CreateResponseStatus(e.GetType().Name, e.Message);

            if (responseStatus == null)
            {
                return(null);
            }

            if (Config.DebugMode)
            {
#if !NETSTANDARD2_0
                if (ex is HttpCompileException compileEx && compileEx.Results.Errors.HasErrors)
                {
                    responseStatus.Errors ??= new List <ResponseError>();
                    foreach (var err in compileEx.Results.Errors)
                    {
                        responseStatus.Errors.Add(new ResponseError {
                            Message = err.ToString()
                        });
                    }
                }
#endif
                // View stack trace in tests and on the client
                var sb = StringBuilderCache.Allocate();

                if (request != null)
                {
                    try
                    {
                        var str = $"[{request.GetType().GetOperationName()}: {DateTime.UtcNow}]:\n[REQUEST: {TypeSerializer.SerializeToString(request)}]";
                        sb.AppendLine(str);
                    }
                    catch (Exception requestEx)
                    {
                        sb.AppendLine($"[{request.GetType().GetOperationName()}: {DateTime.UtcNow}]:\n[REQUEST: {requestEx.Message}]");
                    }
                }

                sb.AppendLine(e.ToString());

                var innerMessages = new List <string>();
                var innerEx       = e.InnerException;
                while (innerEx != null)
                {
                    sb.AppendLine("");
                    sb.AppendLine(innerEx.ToString());
                    innerMessages.Add(innerEx.Message);
                    innerEx = innerEx.InnerException;
                }

                responseStatus.StackTrace = StringBuilderCache.ReturnAndFree(sb);
                if (innerMessages.Count > 0)
                {
                    responseStatus.Meta ??= new Dictionary <string, string>();
                    responseStatus.Meta["InnerMessages"] = innerMessages.Join("\n");
                }
            }

            OnExceptionTypeFilter(e, responseStatus);

            if (Config.DebugMode || Log.IsDebugEnabled)
            {
                OnLogError(GetType(), responseStatus.Message, e);
            }

            return(responseStatus);
        }