Skip to content

Commit

Permalink
Add CreateExceptionProblemDetails and make it virtual
Browse files Browse the repository at this point in the history
  • Loading branch information
khellang committed Aug 8, 2022
1 parent 2528bba commit 0bc61a2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/ProblemDetails/Mvc/ProblemDetailsResultFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void OnResultExecuting(ResultExecutingContext context)
// Set the response status code because it might be used for mapping inside the factory.
context.HttpContext.Response.StatusCode = result.StatusCode ?? StatusCodes.Status500InternalServerError;

var details = Factory.GetDetails(context.HttpContext, exception);
var details = Factory.CreateExceptionProblemDetails(context.HttpContext, exception);

// Devs can choose to ignore errors by returning null.
if (details is null)
Expand Down
23 changes: 13 additions & 10 deletions src/ProblemDetails/ProblemDetailsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public ProblemDetailsFactory(

private ExceptionDetailsProvider DetailsProvider { get; }

public MvcProblemDetails? GetDetails(HttpContext context, Exception error)
[Obsolete("Use " + nameof(CreateExceptionProblemDetails) + " instead.")]
public MvcProblemDetails? GetDetails(HttpContext context, Exception error) => CreateExceptionProblemDetails(context, error);

public virtual MvcProblemDetails? CreateExceptionProblemDetails(HttpContext context, Exception error)
{
MvcProblemDetails? result;
if (error is ProblemDetailsException problem)
Expand Down Expand Up @@ -74,18 +77,18 @@ public ProblemDetailsFactory(
}

return result;
}

private MvcProblemDetails? MapToProblemDetails(HttpContext context, Exception error)
{
if (Options.TryMapProblemDetails(context, error, out var result))
MvcProblemDetails? MapToProblemDetails(HttpContext context, Exception error)
{
// The user has set up a mapping for the specific exception type.
return result;
}
if (Options.TryMapProblemDetails(context, error, out var result))
{
// The user has set up a mapping for the specific exception type.
return result;
}

// Fall back to the generic status code problem details.
return Options.MapStatusCode(context);
// Fall back to the generic status code problem details.
return Options.MapStatusCode(context);
}
}

public override MvcProblemDetails CreateProblemDetails(
Expand Down
2 changes: 1 addition & 1 deletion src/ProblemDetails/ProblemDetailsMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private async Task HandleException(HttpContext context, ExceptionDispatchInfo ed
context.Features.Set<IExceptionHandlerPathFeature>(feature);
context.Features.Set<IExceptionHandlerFeature>(feature);

var details = Factory.GetDetails(context, error);
var details = Factory.CreateExceptionProblemDetails(context, error);

if (details != null) // Don't handle the exception if we can't or don't want to convert it to ProblemDetails
{
Expand Down

0 comments on commit 0bc61a2

Please sign in to comment.