public void When_setting_instance_with_a_relative_uri_should_throw()
        {
            var sut = new HttpProblemDetails { Status = (int)HttpStatusCode.BadRequest };

            Action act = () => sut.Instance = "/relative";

            act.ShouldThrow<InvalidOperationException>();
        }
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            var httpProblemDetailsException   = actionExecutedContext.Exception as IHttpProblemDetailException;
            HttpProblemDetails problemDetails = httpProblemDetailsException != null
                ? httpProblemDetailsException.ProblemDetails
                : _mapProblemDetailsFromException(actionExecutedContext.Exception); // may return null if no mapping from exceptions to problem details has been setup.

            if (problemDetails == null)
            {
                // No problem details to serialize, just a standard intenal server error.
                actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                return;
            }

            var config             = actionExecutedContext.ActionContext.ControllerContext.Configuration;
            var negotiator         = config.Services.GetContentNegotiator();
            var formatters         = config.Formatters;
            var problemDetailsType = problemDetails.GetType();

            ContentNegotiationResult result = negotiator.Negotiate(
                problemDetailsType,
                actionExecutedContext.Request,
                formatters);

            if (result == null) // When the client didn't have appropriate Accept-Type (application/problem+json)
            {
                base.OnException(actionExecutedContext);
                return;
            }

            var status   = problemDetails.Status;
            var response = new HttpResponseMessage((HttpStatusCode)status)
            {
                Content = new ObjectContent(
                    problemDetailsType,
                    problemDetails,
                    result.Formatter,
                    result.MediaType)
            };

            // If a HttpProblemDetailsException or derived was specifically thrown use that type
            // otherwise, because of exception -> problem details mapping, we use HttpProblemDetailsException<>
            var exceptionTypeName = httpProblemDetailsException != null
                ? actionExecutedContext.Exception.GetType().AssemblyQualifiedName
                : typeof(HttpProblemDetailsException <>).MakeGenericType(problemDetailsType).AssemblyQualifiedName;

            // .NET Client will use these custom headers to deserialize and activate the correct types
            response.Headers.Add(CommandClient.HttpProblemDetailsExceptionClrType, exceptionTypeName);
            response.Headers.Add(CommandClient.HttpProblemDetailsClrType, problemDetailsType.AssemblyQualifiedName);

            actionExecutedContext.Response = response;
        }
 public CommandModule()
 {
     For<CommandThatIsAccepted>().Handle(message
         => Console.WriteLine("Command {0} with {1}", message.CommandId, message.Command.Value));
     For<CommandThatThrowsProblemDetailsException>().Handle(_ =>
     {
         var details = new HttpProblemDetails
         {
             Status = (int) HttpStatusCode.BadRequest,
             Title = "A Bad Request",
             Detail = "A command that is not accepted"
         };
         throw new HttpProblemDetailsException<HttpProblemDetails>(details);
     });
 }
 private Type ResolveCommandType(string mediaType)
 {
     try
     {
         return(_settings.ResolveCommandType(mediaType));
     }
     catch (Exception ex)
     {
         var problemDetails = new HttpProblemDetails
         {
             Status = (int)HttpStatusCode.UnsupportedMediaType,
             Detail = ex.Message
         };
         throw new HttpProblemDetailsException <HttpProblemDetails>(problemDetails);
     }
 }
 private Type ResolveCommandType(string mediaType)
 {
     try
     {
         return _settings.ResolveCommandType(mediaType);
     }
     catch(Exception ex)
     {
         var problemDetails = new HttpProblemDetails
         {
             Status = (int) HttpStatusCode.UnsupportedMediaType,
             Detail = ex.Message
         };
         throw new HttpProblemDetailsException<HttpProblemDetails>(problemDetails);
     }
 }
        public void Can_create_exception_with_status_code()
        {
            var sut = new HttpProblemDetails { Status = (int)HttpStatusCode.BadRequest };

            sut.Status.Should().Be((int)HttpStatusCode.BadRequest);
        }