https://github.com/nixjs/error-message
A simple component to control the error message.
https://github.com/nixjs/error-message
error error-handling error-message error-messages errors javascript typescript web
Last synced: 3 months ago
JSON representation
A simple component to control the error message.
- Host: GitHub
- URL: https://github.com/nixjs/error-message
- Owner: nixjs
- Created: 2022-11-04T03:11:17.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-11-04T04:42:03.000Z (over 2 years ago)
- Last Synced: 2025-03-05T17:51:10.102Z (3 months ago)
- Topics: error, error-handling, error-message, error-messages, errors, javascript, typescript, web
- Language: TypeScript
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @nixjs23n6/error-message
A simple component to control the error message.
## Quick Setup
### Install
Install these dependencies:
`yarn add @nixjs23n6/error-message`
### Setup & Usage
```typescript
import { BaseErrorConsts, BaseErrorTypes } from '@nixjs23n6/error-message'export const AuthCode = 20
const enumify = BaseErrorConsts.enumifyError(['WRONG_OTP', 'TOKEN_INVALID_OR_NOT_FOUND'], 0, AuthCode)
export const ourErrors = BaseErrorConsts.enumify({ ...enumify })
export type ErrorType = keyof typeof ourErrors
export const AuthErrors: Record> = {
[ourErrors.WRONG_OTP]: {
type: 'WRONG_OTP',
code: Number(ourErrors.WRONG_OTP),
stringify: (otp: string) => `Wrong OTP ${otp}`,
format: (otp: string) => ({
code: AuthErrors[ourErrors.WRONG_OTP as ErrorType].code,
message: AuthErrors[ourErrors.WRONG_OTP as ErrorType].stringify(otp),
}),
},
[ourErrors.TOKEN_INVALID_OR_NOT_FOUND]: {
type: 'TOKEN_INVALID_OR_NOT_FOUND',
code: Number(ourErrors.TOKEN_INVALID_OR_NOT_FOUND),
stringify: (params: { accessToken: string; refreshToken: string }) =>
`Token invalid or not found ${JSON.stringify(params.accessToken)}`,
format: (params: { accessToken: string; refreshToken: string }) => ({
code: AuthErrors[ourErrors.TOKEN_INVALID_OR_NOT_FOUND as ErrorType].code,
message: AuthErrors[ourErrors.TOKEN_INVALID_OR_NOT_FOUND as ErrorType].stringify(params),
}),
},
}console.log(AuthErrors[ourErrors.WRONG_OTP].format())
console.log(AuthErrors[ourErrors.TOKEN_INVALID_OR_NOT_FOUND].format())
```