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

thegambler

v1.0.21

Published

The Gambler aims to provide a tool for efficiently and accurately handling exceptions in a RESTful application.

Downloads

26

Readme

The Gambler 🃏🃏🃏


Motivação

O objetivo do The Gambler é fornecer uma solução simples para gerar objetos de erro em APIs RESTful construídas em Node.js.

Ele oferece duas classes, a ClientError e a ServerError, cada uma com métodos para gerar objetos de erro personalizados.


Como instalar

$ npm install thegambler

Métodos à disposição

  • badRequest
  • paymentRequired
  • unauthorized
  • forbidden
  • notFound
  • notAcceptable
  • proxyAuthRequired
  • requestTimeout
  • conflict
  • gone
  • lengthRequired
  • preconditionFailed
  • payloadTooLarge
  • uriTooLong
  • unsupportedMediaType
  • rangeNotSatisfiable
  • expectationFailed
  • iAmATeapot
  • misdirectedRequest
  • unprocessableEntity
  • locked
  • failedDependency
  • tooEarly
  • upgradeRequired
  • preconditionRequired
  • tooManyRequests
  • requestHeaderFieldsTooLarge
  • unavailableForLegalReasons
  • internalServerError
  • notImplemented
  • badGateway
  • serviceUnavailable
  • gatewayTimeout
  • httpVersionNotSupported
  • variantAlsoNegotiates
  • insufficientStorage
  • loopDetected
  • notExtended
  • networkAuthenticationRequired

Como manipulá-lo

const { ClientError, ServerError } = require('thegambler');

const clientError = new ClientError();
const serverError = new ServerError();

console.log(clienteError.badRequest('Custom message'));
/*
{
  statusCode: 400,
  message: 'Custom message',
  type: 'Bad Request',
  stack: 'Error\n' +
    '    at Object.<anonymous> (/Users/md/Desktop/teste/index.js:3:21)\n' +
    '    at Module._compile (node:internal/modules/cjs/loader:1105:14)\n' +
    ....
 }
*/

console.log(serverError.internalServerError('Custom message'));
/*
{
  statusCode: 500,
  message: 'Custom message',
  type: 'Internal Server Error',
  stack: 'Error\n' +
    '    at Object.<anonymous> (/Users/md/Desktop/teste/index.js:4:21)\n' +
    '    at Module._compile (node:internal/modules/cjs/loader:1105:14)\n' +
}
*/

/* Ou */

console.log(clienteError.badRequest());
/*
{
  statusCode: 400,
  message: 'Bad Request',
  type: 'Bad Request',
  stack: 'Error\n' +
    '    at Object.<anonymous> (/Users/md/Desktop/teste/index.js:3:21)\n' +
    '    at Module._compile (node:internal/modules/cjs/loader:1105:14)\n' +
...
}
*/

Todos os métodos de ambas classes retornam um objeto de erro com a estrutura a seguir:

| key | type | descrição| |------------|--------------------------------------------|-----------------------------------------------------------------------------------------------| | statusCode | number| O código de resposta que será enviado ao cliente, indicando o status da requisição.| | message | string | A mensagem que será incluída no corpo da resposta, fornecendo informações sobre o erro.| | type | string | Identifica o tipo de erro, facilitando o rastreamento de problemas através do gerenciamento de logs. | | stack | string | É a pilha de chamadas de função que conduziu ao erro, permitindo a investigação detalhada do problema através do gerenciamento de logs. |


Considerando uma API criada com o Express

Exemplo de implementação na camada de Serviço (Service)

const { ClientError } = require('thegambler');
const { userModel } = require('../models');
const { generateToken } = require('../auth');

const login = ({ email, password }) => {
  const clientError = new ClientError();

  const user = userModel.getByEmail(email);
  
  if( user === null || password !== user.password)) {
    throw clientError.badRequest('Invalid Email or Password');
  }

  return { token: generateToken({ email, id: user.id }) } ;
};

Exemplo de gerenciamento de exceções no Controller

const { userService } = require('../services');

const login = async (req, res, next) => {
  const { email, password } = req.body;

  try {
    const { token } = userService.login({ email, password });

    res.status(200).json({ token });
  } catch (error) {

    next(error);
  }
};

Exemplo de manipulação do objeto de erro em um Middleware de Tratamento de Erro

const handlerErrors = (err, _req, res, _next) => {
  console.log(err);

  const statusCode = err.statusCode || 500;

  const message = statusCode !== 500
    ? err.message
    : 'Internal Server Error';

  res.status(statusCode).json({ message });
};

Exemplo de uso com TypeScript

import { NextFunction, Request, Response } from 'express';
import httpStatusCode from 'http-status-codes';
import { ClientError, ServerError } from 'thegambler';
import SaleService from '../../core/services/sale.service';
import {
  ISaleItemsResponse,
  ISaleForRequest,
  ISaleController,
} from '../../types/sales.type';

const { log } = console;

export default class SaleController implements ISaleController {
  private readonly saleService: SaleService;

  private readonly clientError: ClientError;

  private readonly serverError: ServerError;

  constructor() {
    this.saleService = new SaleService();
    this.clientError = new ClientError();
    this.serverError = new ServerError();
  }

  async getSaleById(req: Request, res: Response, next: NextFunction) {
    const { id } = req.params;

    try {
      const sales: ISaleItemsResponse[] | null = await this
      .saleService.getSaleById(Number(id));

      if (sales === null) {
        return next(this.clientError.notFound('Sale not found'));
      }

      return res.status(httpStatusCode.OK).json(sales);
    } catch (error) {
      log(error);

      return next(this.serverError.internalServerError());
    }
  }

Se você encontrar algum erro ou bug no código, por favor, nos ajude abrindo uma issue.

Se desejar contribuir com o projeto, sinta-se à vontade para enviar uma pull request com sugestões de melhorias e novas funcionalidades.

Mantenedores