@qccareerschool/http-status
TypeScript icon, indicating that this package has built-in type declarations

2.1.1 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i @qccareerschool/http-status

Weekly Downloads

0

Version

2.1.1

License

UNLICENSED

Unpacked Size

37.8 kB

Total Files

9

Last publish

Collaborators

  • davewelsh