Comparing version 3.0.0-11 to 3.0.0-12
{ | ||
"name": "erreur", | ||
"version": "3.0.0-11", | ||
"version": "3.0.0-12", | ||
"description": "Type safe custom errors", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -16,6 +16,6 @@ # 🛑 Erreur | ||
```ts | ||
import { createKey, Erreur } from 'erreur'; | ||
import { createErreurType, Erreur } from 'erreur'; | ||
// Create a new key | ||
const StatusCodeKey = createKey<number>({ name: 'StatusCode' }); | ||
// Create a new type | ||
const HttpErrorType = createErreurType<number>({ name: 'StatusCode' }); | ||
@@ -26,6 +26,8 @@ // Create a new Erreur | ||
// Add data to the Erreur | ||
const errWithStatusCode = err.with(StatusCodeKey.Provider(500)); | ||
const errWithStatusCode = HttpErrorType.extends(err, 500); | ||
// Get data from the Erreur | ||
const statusCode = errWithStatusCode.get(StatusCodeKey.Consumer); | ||
const statusCode = errWithStatusCode.get(HttpErrorType.Consumer); | ||
expect(statusCode).toBe(500); | ||
``` | ||
@@ -90,18 +92,2 @@ | ||
### Dynamic message | ||
For a more advanced usage, you can use the `Erreur.GetMessageKey` (or `erreur.withGetMessage`) key to provide a function that will be called when the message is accessed, this function will receive the `Erreur` instance as an argument: | ||
```ts | ||
const StatusCodeKey = createKey<number>({ name: 'StatusCode', defaultValue: 500 }); | ||
const HttpErreur = Erreur.create().withGetMessage((err) => { | ||
return `Something went wrong: ${err.get(StatusCodeKey.Consumer)}`; | ||
}); | ||
const NotFoundErreur = HttpErreur.with(StatusCodeKey.Provider(404)); | ||
console.log(NotFoundErreur.message); // Something went wrong: 404 | ||
``` | ||
## Recipes | ||
@@ -119,3 +105,3 @@ | ||
const FetchErrorKey = createKey<FetchError>({ name: 'FetchError' }); | ||
const FetchErrorType = createErrorType<FetchError>({ name: 'FetchError' }); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18968
104