Giter VIP home page Giter VIP logo

trpc-errormap's Introduction

tRPC Error Map

Provided the HTTP status code, you will get the corresponding tRPC error code.

Installation

npm i trpc-errormap

Usage

import { trpcErrorMap } from 'trpc-errormap';

throw new TRPCError({
  code: trpcErrorMap(404), // returns "NOT_FOUND"
});

Here is the default map object:

400 -> BAD_REQUEST
401 -> UNAUTHORIZED
403 -> FORBIDDEN
404 -> NOT_FOUND
405 -> METHOD_NOT_SUPPORTED
408 -> TIMEOUT
409 -> CONFLICT
412 -> PRECONDITION_FAILED
413 -> PAYLOAD_TOO_LARGE
429 -> TOO_MANY_REQUESTS
499 -> CLIENT_CLOSED_REQUEST
500 -> INTERNAL_SERVER_ERROR

Note: No status code defaults to PARSE_ERROR. You can provide a custom status code for it.

Note: If the status code doesn't match any of the provided codes, it will default to INTERNAL_SERVER_ERROR. You can change the default code.

Customization

You can pass options to trpcErrorMap function:

trpcErrorMap(statusCode, {
  customMap: {
    400: 'PARSE_ERROR', // Override a default code.
    512: 'FORBIDDEN', // Add new codes.
  },

  // Set the default code when there is no match
  defaultError: 'BAD_REQUEST', // default: INTERNAL_SERVER_ERROR
});

You can also create a global custom function:

import { createTrpcErrorMap } from 'trpc-errormap';

const myTrpcErrorMap = createTrpcErrorMap({ customMap: { 512: 'FORBIDDEN' } });

console.log(myTrpcErrorMap(512)); // returns "FORBIDDEN"
console.log(myTrpcErrorMap(512, { customMap: { 512: 'NOT_FOUND' } })); // returns "NOT_FOUND"

trpc-errormap's People

Contributors

ahkhanjani avatar github-actions[bot] avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.