Skip to content
This repository was archived by the owner on Dec 30, 2023. It is now read-only.

Commit f937f27

Browse files
authored
feat(packages): ✨ Add snippets related to Error (#26)
1 parent 70e034e commit f937f27

File tree

20 files changed

+487
-24
lines changed

20 files changed

+487
-24
lines changed

.changeset/young-turtles-kick.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@terminal-nerds/snippets-error": minor
3+
---
4+
5+
✨ Added a new package module group `custom` with the following snippets:
6+
7+
- `isError()`
8+
- `RuntimeError`
9+
- `isRuntimeError()`
10+
- `ValidationError` - powered by [`zod` and `ZodError`](https://github.com/colinhacks/zod)
11+
- `isValidationError()`

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Reusable pieces of code, which can be **reused** across the projects.
100100
| ----------------------------- | ---------------------------------------------- |
101101
| [![snippets badge]][snippets] | [![snippets version badge]][snippets npm page] |
102102
| <!-- PACKAGES --> | <!-- PACKAGES --> |
103+
| [![error badge]][error] | [![error version badge]][error npm page] |
103104
| [![function badge]][function] | [![function version badge]][function npm page] |
104105
| [![test badge]][test] | [![test version badge]][test npm page] |
105106
| [![regexp badge]][regexp] | [![regexp version badge]][regexp npm page] |
@@ -112,6 +113,11 @@ Reusable pieces of code, which can be **reused** across the projects.
112113

113114
<!-- prettier-ignore-start -->
114115
<!-- PACKAGES LINKS -->
116+
[error]: ./packages/error/README.md
117+
[error badge]: https://img.shields.io/static/v1?label=%40terminal-nerds&message=snippets-error&style=flat-square&color=informational
118+
[error version badge]: https://img.shields.io/npm/v/@terminal-nerds/snippets-error/latest?style=flat-square&logo=npm
119+
[error npm page]: https://www.npmjs.com/package/@terminal-nerds/snippets-error
120+
115121
[function]: ./packages/function/README.md
116122
[function badge]: https://img.shields.io/static/v1?label=%40terminal-nerds&message=snippets-function&style=flat-square&color=informational
117123
[function version badge]: https://img.shields.io/npm/v/@terminal-nerds/snippets-function/latest?style=flat-square&logo=npm

packages/error/.depcheckrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"ignores": ["@terminal-nerds/*-config", "tsup", "vitest"]
3+
}

packages/error/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# @terminal-nerds/snippets-error<!-- markdownlint-disable line-length list-marker-space no-duplicate-header ul-style ul-indent no-bare-urls -->

packages/error/LICENSE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/home/xeho91/Nextcloud/Projects/terminal-nerds/snippets/LICENSE.md

packages/error/README.md

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
# @terminal-nerds/snippets-error
2+
3+
![package version badge]
4+
[![size badge]][size url]
5+
[![dependencies badge]][dependencies url]
6+
7+
➡️ **This package wraps all available modules with snippets related to [Error]**
8+
— part of the [terminal-nerds/snippets] project.
9+
10+
[error]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
11+
[terminal-nerds/snippets]: https://github.com/terminal-nerds/snippets
12+
[package version badge]: https://img.shields.io/npm/v/@terminal-nerds/snippets-error/latest?style=for-the-badge&logo=npm
13+
[dependencies badge]: https://img.shields.io/librariesio/release/npm/@terminal-nerds/snippets-error?style=for-the-badge
14+
[dependencies url]: https://libraries.io/npm/@terminal-nerds%2snippets-error
15+
[size badge]: https://img.shields.io/bundlephobia/minzip/@terminal-nerds/snippets-error?style=for-the-badge&label=size
16+
[size url]: https://packagephobia.com/result?p=@terminal-nerds/snippets-error
17+
18+
## Modules included
19+
20+
<!-- prettier-sort-markdown-table -->
21+
22+
| Name | Size |
23+
| ----------------------------------------- | ----------------------------------------------------- |
24+
| [`@terminal-nerds/snippets-error/custom`] | ![custom size gzip badge] ![custom size brotli badge] |
25+
26+
<!-- prettier-ignore-start -->
27+
<!-- MODULES LINKS -->
28+
[`@terminal-nerds/snippets-error/custom`]: https://github.com/terminal-nerds/snippets/blob/main/packages/error/source/custom/custom.ts
29+
[custom size gzip badge]: https://badgen.net/badgesize/gzip/file-url/unpkg.com/@terminal-nerds/snippets-error/dist/custom/custom.js?label=gzip
30+
[custom size brotli badge]: https://badgen.net/badgesize/brotli/file-url/unpkg.com/@terminal-nerds/snippets-error/dist/custom/custom.js?label=brotli
31+
32+
<!-- prettier-ignore-end -->
33+
34+
---
35+
36+
## Prerequisites & usage
37+
38+
### Optional
39+
40+
[![supported typescript version badge]][typescript]
41+
![types badge]
42+
43+
[typescript]: https://typescriptlang.org/
44+
[typescript icon]: https://api.iconify.design/logos/typescript-icon.svg
45+
[supported typescript version badge]: https://img.shields.io/github/package-json/dependency-version/terminal-nerds/snippets/peer/typescript?filename=packages%2Ftypescript%2Fpackage.json&logo=typescript&style=for-the-badge&label=typescript
46+
[types badge]: https://img.shields.io/npm/types/@terminal-nerds/snippets-error?style=for-the-badge&logo=typescript
47+
48+
If you are using ![typescript icon] [TypeScript],
49+
the latest version, which supports new features _(such as `satisfies`)_, is supported.
50+
51+
### Runtime environments
52+
53+
This package can be used in several runtime environments.
54+
We aim for cross-runtime compatibility and ensure proper error messages
55+
if a particular snippet cannot be run in the currently running environment.
56+
57+
#### Browsers
58+
59+
We use [browserslist] to define the minimum browsers versions supported.\
60+
Take a look at our [shared browserslist configuration] for more details.
61+
62+
[browserslist]: https://github.com/browserslist/browserslist
63+
[shared browserslist configuration]: https://github.com/terminal-nerds/configs/blob/main/packages/browserslist/source/browsers.ts
64+
65+
**Usage**:
66+
67+
```html
68+
<script type="module">
69+
import { snippet } from "https://cdn.jsdelivr.net/npm/@terminal-nerds/snippets-error";
70+
</script>
71+
```
72+
73+
---
74+
75+
#### Bun
76+
77+
We aim to support the latest version of ![bun icon] [bun].
78+
79+
**Usage**:
80+
81+
Firstly, install it:
82+
83+
```sh
84+
bun add @terminal-nerds/snippets-error
85+
```
86+
87+
And then in a particular file:
88+
89+
```js
90+
import { snippet } from "@terminal-nerds/snippets-error";
91+
```
92+
93+
[bun]: https://bun.sh/
94+
[bun icon]: https://api.iconify.design/logos/bun.svg
95+
96+
---
97+
98+
#### Deno
99+
100+
We aim to support the latest version of ![deno icon] [Deno].
101+
102+
**Usage**:
103+
104+
```ts
105+
import { snippet } from "npm:@terminal-nerds/snippets-error";
106+
```
107+
108+
[deno]: https://deno.land/
109+
[deno icon]: https://api.iconify.design/logos/deno.svg
110+
111+
---
112+
113+
#### Node.js
114+
115+
[![node.js version support badge]][node.js]
116+
117+
The latest ![node.js icon] [Node.js] LTS _(Long-Term Support)_ version is the minimum one supported.
118+
119+
> **Warning**\
120+
> **This package is written in [ES Module] _(ESM)_ type.**\
121+
> So, if you wish to use it in a project with CommonJS (CJS) type, you need to bundle this package or a particular module(s).
122+
123+
**Usage**:
124+
125+
Install it first with the Node.js package manager of your choice. In our example, we use [pnpm].
126+
127+
```sh
128+
pnpm add @terminal-nerds/snippets-error
129+
```
130+
131+
And then in a particular file:
132+
133+
```js
134+
import { snippet } from "@terminal-nerds/snippets-error";
135+
```
136+
137+
[ES Module]: https://www.freecodecamp.org/news/javascript-es-modules-and-module-bundlers
138+
[pnpm]: https://pnpm.io
139+
[node.js]: https://nodejs.org/en/
140+
[node.js icon]: https://api.iconify.design/logos/nodejs-icon.svg
141+
[node.js version support badge]: https://img.shields.io/node/v-lts/@terminal-nerds/snippets?style=for-the-badge&logo=nodedotjs
142+
143+
---
144+
145+
## Security
146+
147+
[![workflow security badge]][security policy]
148+
149+
🔐 For more information, please refer to the [Security section] at the root of
150+
the [terminal-nerds/snippets] monorepo.
151+
152+
[workflow security badge]: https://img.shields.io/github/actions/workflow/status/terminal-nerds/snippets/maintenance.yml?label=Security&logo=github&style=for-the-badge&branch=main
153+
[security section]: https://github.com/terminal-nerds/snippets#security
154+
[security policy]: https://github.com/terminal-nerds/snippets/security/policy
155+
156+
---
157+
158+
## License
159+
160+
[![license badge]][license]
161+
162+
⚖️ For more information, please refer to the [License section] at the root of the [terminal-nerds/snippets] monorepo.
163+
164+
[license]: https://github.com/terminal-nerds/snippets/blob/main/LICENSE.md
165+
[license badge]: https://img.shields.io/github/license/terminal-nerds/snippets?style=for-the-badge
166+
[license section]: https://github.com/terminal-nerds/snippets#License
167+
168+
### Contributing
169+
170+
[![contributors badge]][contributors url]
171+
172+
🤝 **Contributions of any kind are welcome!**
173+
174+
Please refer to the monorepo _([terminal-nerds/snippets])_ project's [CONTRIBUTING file] for more information
175+
if you wish to get involved.
176+
177+
[contributing file]: https://github.com/terminal-nerds/snippets/blob/main/.github/CONTRIBUTING.md
178+
[contributors badge]: https://img.shields.io/github/contributors/terminal-nerds/snippets?style=for-the-badge
179+
[contributors url]: https://github.com/terminal-nerds/snippets#contributors
180+
181+
### Author
182+
183+
🎉 The idea of this project was initiated by [xeho91]. However, it's the [contributors] who matter the most.
184+
185+
[contributors]: https://github.com/terminal-nerds/snippets/blob/main/README.md#project-contributors
186+
[xeho91]: https://github.com/xeho91

packages/error/package.json

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"$schema": "https://json.schemastore.org/package",
3+
"type": "module",
4+
"name": "@terminal-nerds/snippets-error",
5+
"version": "0.0.0",
6+
"description": "@terminal-nerds reusable snippets for errors.",
7+
"keywords": [
8+
"snippets",
9+
"typescript",
10+
"error"
11+
],
12+
"license": "MIT",
13+
"author": {
14+
"name": "Mateusz Kadlubowski",
15+
"email": "[email protected]",
16+
"url": "https://xeho91.com"
17+
},
18+
"homepage": "https://github.com/terminal-nerds/snippets",
19+
"repository": {
20+
"type": "git",
21+
"url": "https://github.com/terminal-nerds/snippets.git",
22+
"directory": "packages/error"
23+
},
24+
"bugs": "https://github.com/terminal-nerds/snippets/issues",
25+
"engines": {
26+
"node": ">=18"
27+
},
28+
"exports": {
29+
".": {
30+
"types": "./dist/main.d.ts",
31+
"import": "./dist/main.js"
32+
},
33+
"./*": {
34+
"types": "./dist/*/*.d.ts",
35+
"import": "./dist/*/*.js"
36+
}
37+
},
38+
"files": [
39+
"dist/"
40+
],
41+
"scripts": {
42+
"build": "tsup",
43+
"clean": "concurrently \"pnpm:clean:*\" --group --timings",
44+
"clean:build": "del \"./dist\"",
45+
"clean:cache": "del \"./node_modules/.cache\"",
46+
"clean:test": "del \"./coverage\"",
47+
"dev:test": "vitest watch --ui",
48+
"dev:pkg": "tsup --watch",
49+
"fix": "concurrently \"pnpm:fix:*\" --group --timings",
50+
"fix:format": "pretty-quick --branch main --pattern \"packages/error/**/*\" --verbose",
51+
"fix:js": "pnpm lint:js --fix",
52+
"fix:md": "pnpm lint:md --fix",
53+
"fix:pkg": "syncpack format ; syncpack fix-mismatches",
54+
"lint": "concurrently \"pnpm:lint:*\" --group --timings",
55+
"lint:deps": "depcheck",
56+
"lint:format": "pnpm fix:format --check",
57+
"lint:js": "DEBUG=\"eslint:cli-engine\" eslint . --ext .cjs,.js,.ts,.json,.yml --cache --cache-location \"./node_modules/.cache/eslint\"",
58+
"lint:md": "markdownlint . --config \"../../.markdownlint.json\" --dot --ignore \"./LICENSE.md\" --ignore-path \"../../.gitignore\"",
59+
"lint:pkg": "syncpack list-mismatches",
60+
"lint:types": "tsc --noEmit",
61+
"test:unit": "vitest run --dir \"./source\""
62+
},
63+
"dependencies": {
64+
"modern-errors": "5.5.1",
65+
"zod": "3.21.0"
66+
}
67+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { isError, isRuntimeError, isValidationError, RuntimeError, ValidationError } from "./custom.js";
4+
5+
describe("isError(error)", () => {
6+
it(`🔙 returns a boolean 🟢 'true' - on Error`, () => {
7+
expect(isError(new Error("message"))).toBe(true);
8+
});
9+
10+
it(`🔙 returns a boolean 🔴 'false' - on random value`, () => {
11+
expect(isError("")).toBe(false);
12+
});
13+
});
14+
15+
describe("isRuntimeError(error)", () => {
16+
it(`🔙 returns a boolean 🟢 'true' - on RuntimeError`, () => {
17+
expect(isRuntimeError(new RuntimeError("message"))).toBe(true);
18+
});
19+
20+
it(`🔙 returns a boolean 🔴 'false' - on Error`, () => {
21+
expect(isRuntimeError(new Error("message"))).toBe(false);
22+
});
23+
});
24+
25+
describe("isValidationError(error)", () => {
26+
it(`🔙 returns a boolean 🟢 'true' - on ValidationError`, () => {
27+
expect(isValidationError(new ValidationError([]))).toBe(true);
28+
});
29+
30+
it(`🔙 returns a boolean 🔴 'false' - on Error`, () => {
31+
expect(isValidationError(new Error("message"))).toBe(false);
32+
});
33+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* FIXME: There's a bug with pnpm and TypeScript, this a workaround.
3+
*
4+
* @see {@link https://github.com/microsoft/TypeScript/issues/42873}
5+
*/
6+
import type {} from "modern-errors";
7+
import ModernError from "modern-errors";
8+
import { ZodError } from "zod";
9+
10+
export function isError(error: unknown): error is Error {
11+
return error instanceof Error;
12+
}
13+
14+
export const RuntimeError: typeof ModernError = ModernError.subclass("RuntimeError", {
15+
props: {
16+
isRuntimeError: true,
17+
},
18+
});
19+
20+
export function isRuntimeError(error: unknown): error is typeof RuntimeError {
21+
return error instanceof RuntimeError;
22+
}
23+
24+
export const ValidationError = ZodError;
25+
26+
export function isValidationError(error: unknown): error is typeof ValidationError {
27+
return error instanceof ValidationError;
28+
}

packages/error/source/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* MODULES */
2+
export * from "./custom/custom.js";

0 commit comments

Comments
 (0)