/// <inheritdoc />
        public void RaiseException(string errorCode, Exception ex, params object[] args)
        {
            IErrorDetail expDetail = _exceptionDataProvider.GetExceptionDetail(errorCode);

            if (!(expDetail.Clone() is IErrorDetail errorDetail))
            {
                return;
            }

            _errorDetailLocalizer.LocalizeErrorDetail(errorDetail, args);
            if (ex != null)
            {
                _exceptionLogger.LogException(errorDetail, ex);
            }
            RaisedException exception     = new RaisedException(errorDetail.Message, ex);
            ExceptionData   exceptionData = HandleException(errorDetail);

            if (exceptionData != null)
            {
                foreach (string key in exceptionData.Keys)
                {
                    exception.Data[key] = exceptionData[key];
                }
            }
            throw exception;
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public override ExceptionData HandleException(IErrorDetail detail, Exception ex = null)
        {
            ExceptionData exceptionData = base.HandleException(detail, ex);

            exceptionData.Message = ex == null ? detail.WrapMessage : string.Format(detail.WrapMessage, ex.Message);
            return(exceptionData);
        }
 /// <inheritdoc />
 public virtual IActionResult CreateActionResult(IErrorDetail details, IDictionary <string, object> data)
 {
     return(new ObjectResult(data)
     {
         StatusCode = (int)details.StatusCode
     });
 }
Ejemplo n.º 4
0
        public IResult <TData> NullObjectError <TData>(IErrorDetail detail, string operation, string target = "")
        {
            IErrorBuilder error = DefaultError <TData>(CommonErrorCode.NullObjectError,
                                                       CommonErrorType.NullObjectError,
                                                       CommonErrorMessage.NullObjectError(operation),
                                                       target);

            return(DefaultResult <TData>(error, detail, CommonErrorCode.NullObjectError));
        }
Ejemplo n.º 5
0
        public IResult <TData> TransportError <TData>(IErrorDetail detail, string target = "")
        {
            IErrorBuilder error = DefaultError <TData>(CommonErrorCode.TransportError,
                                                       CommonErrorType.TransportError,
                                                       CommonErrorMessage.TransportError,
                                                       target);

            return(DefaultResult <TData>(error, detail, CommonErrorCode.ValidationError));
        }
Ejemplo n.º 6
0
        public IResult <TData> IntegrationError <TData>(IErrorDetail detail, string service, string target = "")
        {
            IErrorBuilder error = DefaultError <TData>(CommonErrorCode.IntegrationError,
                                                       CommonErrorType.IntegrationError,
                                                       CommonErrorMessage.IntegrationError(service),
                                                       target);

            return(DefaultResult <TData>(error, detail, CommonErrorCode.IntegrationError));
        }
Ejemplo n.º 7
0
        public IResult <TData> DatabaseError <TData>(IErrorDetail detail, string database, string target = "")
        {
            IErrorBuilder error = DefaultError <TData>(CommonErrorCode.DatabaseError,
                                                       CommonErrorType.DatabaseError,
                                                       CommonErrorMessage.DatabaseError(database),
                                                       target);

            return(DefaultResult <TData>(error, detail, CommonErrorCode.DatabaseError));
        }
 /// <inheritdoc />
 public override ExceptionData HandleException(IErrorDetail detail, Exception ex = null)
 {
     return(new ExceptionData
     {
         ExceptionCode = detail.Name,
         Message = ex == null ? detail.Message : string.Format(detail.Message, ex.Message),
         EventId = detail.EventId.Id,
         EventName = detail.EventId.Name
     });
 }
Ejemplo n.º 9
0
        private IResult <TData> DefaultResult <TData>(IErrorBuilder builder, IErrorDetail detail, string errorCode)
        {
            var statusCode = Convert.ToInt32(errorCode);

            if (detail is null)
            {
                return(new Result <TData>().AddError(builder.Build()).AddStatusCode(statusCode));
            }

            builder.WithDetail(detail);
            return(new Result <TData>().AddError(builder.Build()).AddStatusCode(statusCode));
        }
 /// <inheritdoc />
 public void LocalizeErrorDetail(IErrorDetail errorDetail, object[] args)
 {
     if (_exceptionDataProvider.LocalizationEnabled)
     {
         errorDetail.Message = _errorDetailLocalizer[errorDetail.Message];
     }
     try
     {
         errorDetail.Message = args == null || args.Length < 1 ? errorDetail.Message : string.Format(errorDetail.Message, args);
     }
     catch
     {
         //Supress the exception
     }
 }
        /// <inheritdoc />
        public override ExceptionData HandleException(IErrorDetail detail, Exception ex = null)
        {
            ExceptionData exceptionData = base.HandleException(detail, ex);

            if (ex == null)
            {
                ex = new RaisedException(exceptionData.Message);
            }
            foreach (string key in exceptionData.Keys)
            {
                ex.Data[key] = exceptionData[key];
            }

            // TODO remove return statement and implement logic to throw ex
            return(exceptionData);
            // throw ex;
        }
Ejemplo n.º 12
0
        /// <inheritdoc />
        public virtual IActionResult CreateActionResult(IErrorDetail details, IDictionary <string, object> data)
        {
            var viewResult = new ViewResult
            {
                StatusCode = (int)details.StatusCode,
                ViewName   = details.ViewName,
                ViewData   = new ViewDataDictionary(_modelMetadataProvider, new ModelStateDictionary())
            };

            if (data != null)
            {
                foreach (var key in data.Keys)
                {
                    viewResult.ViewData[key] = data[key];
                }
            }
            return(viewResult);
        }
Ejemplo n.º 13
0
        private ExceptionData HandleException(IErrorDetail errorDetail, Exception ex = default)
        {
            IExceptionHandler exceptionHandler = _exceptionDataProvider.GetExceptionHandler(errorDetail.HandlerName);

            return(exceptionHandler.HandleException(errorDetail, ex));
        }
Ejemplo n.º 14
0
        /// <inheritdoc />
        public void LogException(IErrorDetail detail, Exception ex = null)
        {
            if (_exceptionDataProvider.LoggingEnabled)
            {
                var eventId = new Microsoft.Extensions.Logging.EventId(detail.EventId.Id, detail.EventId.Name);

                switch (detail.LogLevel)
                {
                case LogLevel.Critical:
                    if (ex == null)
                    {
                        _exceptionLogger.LogCritical(eventId, detail.Message);
                    }
                    else
                    {
                        _exceptionLogger.LogCritical(eventId, ex, detail.Message);
                    }
                    break;

                case LogLevel.Debug:
                    if (ex == null)
                    {
                        _exceptionLogger.LogDebug(eventId, detail.Message);
                    }
                    else
                    {
                        _exceptionLogger.LogDebug(eventId, ex, detail.Message);
                    }
                    break;

                case LogLevel.Error:
                    if (ex == null)
                    {
                        _exceptionLogger.LogError(eventId, detail.Message);
                    }
                    else
                    {
                        _exceptionLogger.LogError(eventId, ex, detail.Message);
                    }
                    break;

                case LogLevel.Information:
                    if (ex == null)
                    {
                        _exceptionLogger.LogInformation(eventId, detail.Message);
                    }
                    else
                    {
                        _exceptionLogger.LogInformation(eventId, ex, detail.Message);
                    }
                    break;

                case LogLevel.Warning:
                    if (ex == null)
                    {
                        _exceptionLogger.LogWarning(eventId, detail.Message);
                    }
                    else
                    {
                        _exceptionLogger.LogWarning(eventId, ex, detail.Message);
                    }
                    break;

                case LogLevel.Trace:
                    if (ex == null)
                    {
                        _exceptionLogger.LogTrace(eventId, detail.Message);
                    }
                    else
                    {
                        _exceptionLogger.LogTrace(eventId, ex, detail.Message);
                    }
                    break;

                case LogLevel.None:
                default:
                    break;
                }
            }
        }
Ejemplo n.º 15
0
 /// <summary>
 ///     Adds an <see cref="ErrorDetail" /> into the details list.
 /// </summary>
 /// <param name="detailedError">The detail of error</param>
 /// <returns>
 ///     <see cref="Error" />
 /// </returns>
 public static IError AddErrorDetail(this IError error, IErrorDetail detailedError)
 {
     error.Details.Add(detailedError);
     return(error);
 }
Ejemplo n.º 16
0
 public IErrorBuilder WithDetail(IErrorDetail detail)
 {
     _details.Add(detail);
     return(this);
 }