/// <summary> /// Create an HttpResponseMessage given an RFC7807ProblemDetail instance /// </summary> public static HttpResponseMessage CreateRFC7807ProblemResponse(this HttpRequestMessage request, RFC7807ProblemDetail detail) { var media = RFC7807Media.GetRFC7807ContentTypeForRequest(request); var formatter = RFC7807Media.GetMediaTypeFormatterForResponseType( request.GetConfiguration(), media); var exception = new RFC7807Exception(detail); return(request.CreateResponse((HttpStatusCode)exception.ProblemDetail.Status, detail, formatter, media)); }
/// <summary> /// Create an IHttpActionResult given an RFC7807ProblemDetail instance /// </summary> public static IHttpActionResult CreateRFC7807ProblemActionResult(this HttpRequestMessage request, RFC7807ProblemDetail detail) { var media = RFC7807Media.GetRFC7807ContentTypeForRequest(request); var formatter = RFC7807Media.GetMediaTypeFormatterForResponseType( request.GetConfiguration(), media); var exception = new RFC7807Exception(detail); return(new FormattedContentResult <RFC7807ProblemDetail>( (HttpStatusCode)exception.ProblemDetail.Status, detail, formatter, new MediaTypeHeaderValue(media), request)); }
/// <summary> /// Constructor taking any Exception instance (including derived subclasses), /// and optionally an instance URI (perhaps the request URI) that will be /// stored to the Instance field, and retrieves /// RFC7807 problem detail from the exception data. /// </summary> /// <remarks> /// The resulting instance will have an http status code of either 500, /// or 501 for a NotImplementedException. /// </remarks> public RFC7807Exception(Exception ex, Uri instanceUri = null) { ProblemDetail = new RFC7807ProblemDetail { Status = (int)HttpStatusCode.InternalServerError, Type = new Uri(TypeUriAuthority + ex.GetType().FullName), Detail = ex.Message, Instance = instanceUri }; // 501 errors can be determined directly from the exception type if (ex is NotImplementedException) { ProblemDetail.Status = (int)HttpStatusCode.NotImplemented; } }
/// <summary> /// Constructor needing only an HttpStatusCode. /// </summary> /// <remarks> /// The instance returned meets the requirements but not necessarily the intent /// of RFC7807 so you should really only use this version when you plan to flesh /// out the <see cref="ProblemDetail"/> in subsequent code, unless the /// http status code alone really is completely sufficient to describe the problem. /// </remarks> public RFC7807Exception(HttpStatusCode statusCode) { ProblemDetail = new RFC7807ProblemDetail(statusCode); }