@qccareerschool/http-status
v2.1.1
Published
Provides HTTP response codes and custom sub-classes of the Error class that represent HTTP errors.
Downloads
6
Readme
Synopsis
Provides HTTP response codes and custom sub-classes of the Error class that represent HTTP errors.
Code Example
import * as HttpStatus from '@qccareerschool/http-status';
import pool from './pool';
app.get('/countries', async (req, res): void => {
try {
const connection = await pool.getConnection();
try {
const countries = await connection.query('SELECT code, name FROM countries');
if (countries.length === 0) {
throw new HttpStatus.InternalServerError('No countries found in database');
}
res.send(countries);
} finally {
pool.releaseConnection(connection);
}
} catch(err) {
if (err instanceof HttpStatus.HttpResponse && err.isClientError()) {
res.status(err.getStatusCode()).send({ message: err.message });
return;
}
console.log(err);
res.status(HttpStatus.INTERNAL_SERVER_ERROR).send({ error: err, message: err.message });
}
}
Installation
npm i @qccareerschool/http-status