public static async Task <object> CreateError(ITypedBuilderContext context, HttpRequestMessage request, HttpResponseMessage response, Exception exception) { var allowNullError = typeof(IEmptyError).IsAssignableFrom(context.ErrorType); object error = null; if (!allowNullError && response != null) { try { error = await context.Formatter.DeserializeError(response, context); } catch (Exception ex) { exception = exception != null ? new AggregateException(ex, exception) : ex; } } if (error == null) { error = exception != null ? context.DefaultErrorFactory?.Invoke(context.ErrorType, exception) : TypeHelpers.GetDefaultValueForType(context.ErrorType); } if (error == null && !allowNullError && exception != null) { throw exception; } var errorWithMeta = error as IResultWithWritableMetadata; if (errorWithMeta != null) { errorWithMeta.Metadata = CachingHelpers.CreateResponseMetadata(error, request, response, context.CacheMetadata); } return(error); }
public static async Task <object> CreateResult(ITypedBuilderContext context, HttpRequestMessage request, HttpResponseMessage response) { object result; if (typeof(IEmptyResult).IsAssignableFrom(context.ResultType)) { result = TypeHelpers.GetDefaultValueForType(context.ResultType); } else { result = await context.Formatter.DeserializeResult(response, context); } var resultWithMeta = result as IResultWithWritableMetadata; if (resultWithMeta != null) { resultWithMeta.Metadata = CachingHelpers.CreateResponseMetadata(result, request, response, context.CacheMetadata); } return(result); }