npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

get-problem-details

v2.0.0

Published

Extracts the Problem Details (RFC 7807) object from an unsuccessful server response (fetch/HttpErrorResponse). Eases the extraction of the Problem Details from a non 200 response in a client application.

Downloads

70

Readme

Get Problem Details

A lightweight library that extracts the problem details (RFC 7807) object from an unsuccessful server response. Easily maps a typed Problem Details object to be used for handling unsuccessful responses from an API that may implement the Problem Details web specification RFC 7807. Designed to be use with fetch and a client application making a server call such as HttpClient from Angular.

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 get-problem-details

Import the ProblemDetails class

import {ProblemDetails} from "get-problem-details";

Examples

Extracting as many Problem Details properties from a failed fetch response

After parsing a response, create a new ProblemDetails class and pass the parsed response to the constructor to map the ProblemDetails object from the backend server. 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 = new ProblemDetails(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 = new ProblemDetails(response, errMsg);

    problemToast(problemDetails);   // Custom toast not provided.
}

Extracting Problem Details from a failed HttpErrorResponse in Angular

Catch the error from an HttpClient error and pass the error property to obtain the problem details object from your server. You can then log this object and or display the failed response to the user.

    this.myHttpService.getData()
    .pipe(
        catchError(err => {
            // Do something with your problem details object, 
            // such as making a toast for your users to display a failed HttpErrorResponse.
            const problemDetails = new ProblemDetails(err.error)
            console.error(problemDetails);
            this.snackbarService.problemDetails(problemDetails); // Custom toast not provided.
        // ...
        })
    );

Problem Details Object Properties

interface ProblemDetailsI {
    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.