/// <summary> /// Creates the error data from the values provided. /// /// If the errorCode is empty it will use the first validation error code, /// if there is none it will throw an error. /// </summary> /// <param name="errorCode">The error code.</param> /// <param name="errorMessage">The error message.</param> /// <param name="validationErrors">The validation errors.</param> /// <returns></returns> public static ErrorDataType CreateErrorData(string errorCode, string errorMessage, IEnumerable <ValidationErrorField> validationErrors) { var to = new ErrorDataType { ErrorCode = errorCode, Message = errorMessage, ErrorFields = new List <ErrorFieldType>(), }; if (validationErrors != null) { foreach (var validationError in validationErrors) { var error = new ErrorFieldType { ErrorCode = validationError.ErrorCode, FieldName = validationError.FieldName, Message = validationError.ErrorMessage, }; to.ErrorFields.Add(error); if (string.IsNullOrEmpty(to.ErrorCode)) { to.ErrorCode = validationError.ErrorCode; } if (string.IsNullOrEmpty(to.Message)) { to.Message = validationError.ErrorMessage; } } } if (string.IsNullOrEmpty(errorCode)) { if (string.IsNullOrEmpty(to.ErrorCode)) { throw new ArgumentException("Cannot create a valid error response with an empty errorCode and an empty validationError list"); } } return(to); }
/// <summary> /// Create SLA Error response /// </summary> /// <param name="hystrixEvent">the hystrix event caused the error</param> /// <param name="responseType">target response type</param> /// <returns></returns> public static object CreateSLAErrorResponse(HystrixEventType hystrixEvent, Type responseType) { var error = new ErrorDataType(); if (hystrixEvent == HystrixEventType.ShortCircuited) { error.Message = "Server entered into self-protecting mode"; error.ErrorCode = hystrixEvent.ToString(); } if (hystrixEvent == HystrixEventType.ThreadPoolRejected) { error.Message = "Server entered into rate-limiting mode"; error.ErrorCode = hystrixEvent.ToString(); } error.SeverityCode = SeverityCodeType.Error; error.ErrorClassification = ErrorClassificationCodeType.SLAError; var errors = new List <ErrorDataType>() { error }; var errorResponse = CreateErrorResponseDto(errors, responseType); if (HostContext.Instance.Request != null) { var metadata = EndpointHost.MetadataMap[HostContext.Instance.Request.ServicePath]; var additionalData = new Dictionary <string, string>(); additionalData.Add("Service", metadata.FullServiceName); additionalData.Add("Operation", HostContext.Instance.Request.OperationName); additionalData.Add("ErrorCode", "FXD300005"); Log.Error("SLA error occurred: Circuit Breaker is open.", additionalData); } return(errorResponse); }