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

@poppanator/http-error

v3.0.2

Published

This is a utility error class for representing a HTTP error (or status).

Downloads

15

Readme

HTTP error class

This is a utility error class for representing a HTTP error (or status).

Install

# NPM
npm i @poppanator/http-error

# Yarn
yarn add @poppanator/http-error

Usage

HttpError

Properties

These are the properties exposed, other than the default ones inherited from the Error class.

  • statusCode: The HTTP status code, e.g. 400, 404, 500 etc.
  • statusText: The HTTP status text, e.g. Not Found, Internal Server Error etc
  • fullStatusMessage: The HTTP status and text, e.g. 404 Not Found
import { HttpError } from '@poppanator/http-error'

const err1 = new HttpError(307)

err1.statusCode // 307
err1.statusText // Temporary Redirect
err1.message // 307 Temporary Redirect
err1.fullStatusMessage // 307 Temporary Redirect

const err2 = new HttpError(404, 'Lost Some Where')

err2.statusCode // 404
err2.statusText // Lost Some Where
err2.message // 404 Lost Some Where
err2.fullStatusMessage // 404 Lost Some Where

const err3 = new HttpError(500, { message: `That's not good` })

err3.statusCode // 500
err3.statusText // Internal Server Error
err3.message // That's not good
err3.fullStatusMessage // 500 Internal Server Error

const err3 = new HttpError(500, {
  statusText: 'Server Broke',
  message: `That's not good`,
})

err3.statusCode // 500
err3.statusText // Server Broke
err3.message // That's not good
err3.fullStatusMessage // 500 Server Broke

This is the constructor signature:

type HttpErrorConstuctorArgs = {
  /** Status text */
  statusText?: string
  /** Error message */
  message?: string
}

/**
 * Creates a new `HttpError` instance
 *
 * @param statusCode
 *  The HTTP status code, e.g. `301`, `400`, `404`, `500` etc
 * @param statusTextOrArgs -
 *   - If `undefined` the status text will be automatically resolved to the
 *     `statusCodes`'s corresponding status text, e.g. `OK` for `200`,
 *     `Not Found` for `404`, `Internal Server Error` for `500` etc.
 *
 *   - If a `string` the value will be used as status text
 *   - If a {@link HttpErrorConstuctorArgs} object the status text and message
 *     will be potentially revolved from there.
 */
constructor(
  statusCode: number,
  statusTextOrArgs?: string | HttpErrorConstuctorArgs
)

Utility functions

isHttpError()

Check if something is an instance of HttpError

import { isHttpError } from '@poppanator/http-error'

try {
  await doSomethingThatMightThrow()
} catch (err: unknown) {
  if (isHttpError(err)) {
    logger.warn(`Got HTTP error: ${err.fullStatusMessage}`)
  } else {
    logger.warn(`Got error: ${err.message}`)
  }
}

This is the method signature:

export function isHttpError(obj: unknown): obj is HttpError

resolveHttpStatusCode()

When dealing with HTTP client libraries you might end up with a HTTP error object of sort, but it might not always be clear how to get hold of the HTTP status code.

This function tries to resolve the status code by looking at various properties with common status code names.

By default the function will check for the code, status and statusCode properties and return the value of any of these if the value is a number.

You can also pass additional properties to check in otherProperties

import { resolveHttpStatusCode } from '@poppanator/http-error'

const withCode = { code: 201 }
resolveHttpStatusCode(withCode) // 201

const withStatus = { status: 201 }
resolveHttpStatusCode(withStatus) // 201

const withStatusCode = { statusCode: 201 }
resolveHttpStatusCode(withStatusCode) // 201

const withNonNumeric = { code: '201' }
resolveHttpStatusCode(withNonNumeric) // undefined

const withOutKnownProperty = { prop: 201 }
resolveHttpStatusCode(withOutKnownProperty) // undefined

const withOutKnownProperty = { prop: 201 }
resolveHttpStatusCode(withOutKnownProperty, ['prop']) // 201

This is the method signature:

type PlainObject<T = any> = { [key: string]: T }
export type IsStatusCodeLike = number | PlainObject

/**
 * @param arg - Something with maybe a status code
 * @param otherProperties - Additional properties that might represent a
 *  status code
 * @returns The resolved status code or `undefined` if none was found
 */
export function resolveHttpStatusCode<T extends string[]>(
  arg: IsStatusCodeLike,
  otherProperties?: T
): Maybe<number>