Beispiel #1
0
            /// <summary>
            /// Reads the JSON representation of the object.
            /// </summary>
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                if (reader.TokenType == JsonToken.Null)
                {
                    return(null);
                }

                MatchTokenOrThrow(reader, JsonToken.StartObject);
                ReadOrThrow(reader);

                Type            valueType = objectType.IsConstructedGenericType ? objectType.GenericTypeArguments[0] : null;
                object          value     = null;
                ServiceErrorDto error     = null;

                while (reader.TokenType == JsonToken.PropertyName)
                {
                    string propertyName = (string)reader.Value;
                    ReadOrThrow(reader);

                    if (string.Equals(propertyName, c_valuePropertyName, StringComparison.OrdinalIgnoreCase))
                    {
                        if (valueType == null)
                        {
                            throw new JsonSerializationException("ServiceResult does not support 'value'; use ServiceResult<T>.");
                        }

                        value = serializer.Deserialize(reader, valueType);
                    }
                    else if (string.Equals(propertyName, c_errorPropertyName, StringComparison.OrdinalIgnoreCase))
                    {
                        error = serializer.Deserialize <ServiceErrorDto>(reader);
                    }

                    ReadOrThrow(reader);
                }

                MatchTokenOrThrow(reader, JsonToken.EndObject);

                if (value != null && error != null)
                {
                    throw new JsonSerializationException("ServiceResult must not have both 'value' and 'error'.");
                }

                if (valueType == null)
                {
                    return(error != null?Failure(error) : Success());
                }
                else if (error != null)
                {
                    return((ServiceResult)s_genericCastMethod.MakeGenericMethod(valueType).Invoke(Failure(error), new object[0]));
                }
                else
                {
                    if (value == null && valueType.GetTypeInfo().IsValueType)
                    {
                        value = Activator.CreateInstance(valueType);
                    }
                    return((ServiceResult)s_genericSuccessMethod.MakeGenericMethod(valueType).Invoke(null, new[] { value }));
                }
            }
Beispiel #2
0
        /// <summary>
        /// Creates an exception from an error data object.
        /// </summary>
        public ServiceException(ServiceErrorDto error)
        {
            if (error == null)
            {
                throw new ArgumentNullException(nameof(error));
            }

            m_error = error;
        }
Beispiel #3
0
        /// <summary>
        /// Creates a failed result.
        /// </summary>
        public static ServiceResultFailure Failure(ServiceErrorDto error)
        {
            if (error == null)
            {
                throw new ArgumentNullException(nameof(error));
            }

            return(new ServiceResultFailure(error));
        }
Beispiel #4
0
        /// <summary>
        /// Creates an exception from an error data object and an inner exception.
        /// </summary>
        public ServiceException(ServiceErrorDto error, Exception innerException)
            : base(null, innerException)
        {
            if (error == null)
            {
                throw new ArgumentNullException(nameof(error));
            }

            m_error = error;
        }
Beispiel #5
0
        private static string GetErrorString(ServiceErrorDto error, string indent = "")
        {
            string text = error.Message ?? "";

            if (error.Code != null)
            {
                text += " (" + error.Code + ")";
            }

            if (error.Details != null)
            {
                text += Environment.NewLine + indent + "  Details: " + error.Details;
            }

            if (error.InnerError != null)
            {
                text += Environment.NewLine + indent + "  InnerError: " + GetErrorString(error.InnerError, indent + "  ");
            }

            return(text);
        }
Beispiel #6
0
 /// <summary>
 /// Creates an exception from an error data object.
 /// </summary>
 public ServiceException(ServiceErrorDto error)
 {
     m_error = error ?? throw new ArgumentNullException(nameof(error));
 }
Beispiel #7
0
 internal ServiceResultFailure(ServiceErrorDto error)
     : base(error)
 {
 }
Beispiel #8
0
 internal ServiceResult(ServiceErrorDto error)
 {
     m_error = error;
 }
 /// <summary>
 /// Creates a failed result.
 /// </summary>
 public static ServiceResultFailure Failure(ServiceErrorDto error) => new ServiceResultFailure(error ?? throw new ArgumentNullException(nameof(error)));
 /// <summary>
 /// Creates an exception from an error data object and an inner exception.
 /// </summary>
 public ServiceException(ServiceErrorDto error, Exception?innerException)
     : base(null, innerException)
 {
     Error = error ?? throw new ArgumentNullException(nameof(error));
 }