Http errors package for Typescript
Hacktoberfest 5 has begon, and as my first contribution I wanted to make a tiny Typescript library with HTTP errors.
Whenever I start a new Javascript-based project, whether that’s on the server, or if I’m writing an API client with Fetch, I often find myself doing the same thing over and over again, which is to define a simple set of exceptions representing HTTP errors, like this:
class NotFound extends Error {
httpStatus = 404;
}
A bit fed up with this, I decided to make a small package that’s just a list of errors for Typescript, along with some tiny utilities.
Example:
import { NotFound } from '@curveball/http-errors';
throw new NotFound('Article not found');
The idea is that the interface is really just this:
export interface HttpError extends Error {
httpStatus: number;
}
Which means that any error with a httpStatus
property automatically follows
this pattern, and generic middlewares can be written to listen for them.
It comes with a simple utility function to see if the Error
conforms with
this pattern:
import { isHttpError } from '@curveball/http-errors';
const myError = new Error('Custom error');
myError.httpStatus = 500;
console.log(isHttpError(myError)); // true
Problem+json
A great idea for emitting errors from a HTTP API is to use the
application/problem+json
format, defined in RFC7807. The package also
contains a few utilities to help with these:
export interface HttpProblem extends HttpError {
type: string | null;
title: string;
detail: string | null;
instance: string | null;
}
Every standard exception that ships with this package also implements this
interface. Most properties (except title
) default to NULL
as they are likely
applica
Truncated by Planet PHP, read more at the original (another 3511 bytes)