@emoyly/problem v7.0.4
Problem
UNDER DEVELOPMENT - NOT READY FOR PRODUCTION
Problem is an error handling library, based around the RFC-7807 standard, with support for additional parsers and middleware (Explained below). The library may not follow the standard completely in some cases. An example of which is that it currently only supports the JSON variant of problem details. There is also other features added, making it much more of an HTTP error handling framework, than just an implementation of the standard.
Terms
Middleware
Middleware catches errors occurring in a node module, like Express or Axios, and converts it into a Problem object.
Parsers
A parser converts common JS errors from various npm modules into Problem objects.
Usage & examples
Basic
TODO: Write a basic explanation of just implementing the library somehow
Express
import { ExpressMiddleware } from '@emoyly/problem/middleware/express'
const problemMiddleware = new ExpressMiddleware()
events.onProblem(problems => {
for (const problem of problems) {
// If its a 4XX error or alike, we don't really need to log it
if (problem.status < 500) continue
// Log the error. This could be replaced with a function reporting the error to something like Sentry
console.error(problem)
}
})
// In your Express middleware ↓
// Handles 404 errors
app.use(problemMiddleware.notFound)
// Responds in the right way
app.use(problemMiddleware.use())
Axios
import axios from 'axios'
import { Problem } from '@emoyly/problem'
import { AxiosMiddleware } from '@emoyly/problem/middleware/axios'
const api = axios.create({
'baseURL': `example.com`,
})
const middleware = new AxiosMiddleware()
api.interceptors.response.use(...middleware.use())
1 year ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago