1.0.2 • Published 11 months ago
extract-problem-details v1.0.2
Extract Problem Details
A lightweight library that extracts the problem details (RFC 7807) object from an unsuccessful response. Easily maps a typed Problem Details object to be used for handling unsuccessful responses from an API that implements the Problem Details web specification RFC 7807.
Response objects may lack problem details properties or have none at all, in which case they default to a problem
details object with status: 500
and title: "Server Error"
.
Usage
Install npm package
npm i extract-problem-details
Import getProblemDetails and ProblemDetails type
import getProblemDetails from "extract-problem-details";
import type {ProblemDetails} from "extract-problem-details";
Examples
Extracting as many Problem Details properties from a failed response
After parsing a response, call the getProblemDetails
function and pass the parsed response to
getProblemDetails to obtain a ProblemDetails object. You can then log this object or display the failed response to the user.
if (!response.ok) {
const responseResult = await response.json() as unknown;
/************************************************************************/
const problemDetails: ProblemDetails = getProblemDetails(responseResult);
/************************************************************************/
// Do something with your problem details object,
// such as making a toast for your users to display a failed response.
problemToast(problemDetails); // Custom toast not provided.
}
Getting a Problem Details object from a produced exception while fetching
let response: Response | undefined;
try {
// Your fetch logic...
} catch (e) {
// Create your own getErrorMessage to extract error message or set a default error message.
const errMsg = getErrorMessage(e);
/*************************************************************************/
const problemDetails: ProblemDetails = getProblemDetails(response, errMsg);
/*************************************************************************/
problemToast(problemDetails); // Custom toast not provided.
}
Problem Details Object
interface ProblemDetails {
type?: string;
title: string;
status: number;
detail?: string;
traceId?: string;
instance?: string;
// Default problem details extention in ASP.NET
errors?: Record<string, string[]>
}
TODO
- Add functionality for custom problem details extensions to replace errors.
1.0.2
11 months ago