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

Commit 9afe9b9

Browse files
authored
feat(packages/error): ✨ Add snippets related to built-in errors (#27)
1 parent f937f27 commit 9afe9b9

File tree

8 files changed

+213
-29
lines changed

8 files changed

+213
-29
lines changed

.changeset/three-donkeys-carry.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
"@terminal-nerds/snippets-error": minor
3+
---
4+
5+
✨ Added a new package module group `built-in` with the following snippets:
6+
7+
- `ERROR_SCHEMA`
8+
- `isError()`
9+
- `EVAL_ERROR_SCHEMA`
10+
- `isEvalError()`
11+
- `RANGE_ERROR_SCHEMA`
12+
- `isRangeError()`
13+
- `REFERENCE_ERROR_SCHEMA`
14+
- `isReferenceError()`
15+
- `SYNTAX_ERROR_SCHEMA`
16+
- `isSyntaxError()`
17+
- `TYPE_ERROR_SCHEMA`
18+
- `isTypeError()`
19+
- `URI_ERROR_SCHEMA`
20+
- `isURIError()`

.changeset/young-turtles-kick.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
✨ Added a new package module group `custom` with the following snippets:
66

7-
- `isError()`
87
- `RuntimeError`
8+
- `RUNTIME_ERROR_SCHEMA`
99
- `isRuntimeError()`
1010
- `ValidationError` - powered by [`zod` and `ZodError`](https://github.com/colinhacks/zod)
11+
- `VALIDATION_ERROR_SCHEMA`
1112
- `isValidationError()`

packages/error/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@
1919

2020
<!-- prettier-sort-markdown-table -->
2121

22-
| Name | Size |
23-
| ----------------------------------------- | ----------------------------------------------------- |
24-
| [`@terminal-nerds/snippets-error/custom`] | ![custom size gzip badge] ![custom size brotli badge] |
22+
| Name | Size |
23+
| ------------------------------------------- | --------------------------------------------------------- |
24+
| [`@terminal-nerds/snippets-error/built-in`] | ![built-in size gzip badge] ![built-in size brotli badge] |
25+
| [`@terminal-nerds/snippets-error/custom`] | ![custom size gzip badge] ![custom size brotli badge] |
2526

2627
<!-- prettier-ignore-start -->
2728
<!-- MODULES LINKS -->
29+
[`@terminal-nerds/snippets-error/built-in`]: https://github.com/terminal-nerds/snippets/blob/main/packages/error/source/built-in/built-in.ts
30+
[built-in size gzip badge]: https://badgen.net/badgesize/gzip/file-url/unpkg.com/@terminal-nerds/snippets-error/dist/built-in/built-in.js?label=gzip
31+
[built-in size brotli badge]: https://badgen.net/badgesize/brotli/file-url/unpkg.com/@terminal-nerds/snippets-error/dist/built-in/built-in.js?label=brotli
32+
2833
[`@terminal-nerds/snippets-error/custom`]: https://github.com/terminal-nerds/snippets/blob/main/packages/error/source/custom/custom.ts
2934
[custom size gzip badge]: https://badgen.net/badgesize/gzip/file-url/unpkg.com/@terminal-nerds/snippets-error/dist/custom/custom.js?label=gzip
3035
[custom size brotli badge]: https://badgen.net/badgesize/brotli/file-url/unpkg.com/@terminal-nerds/snippets-error/dist/custom/custom.js?label=brotli
31-
3236
<!-- prettier-ignore-end -->
3337

3438
---
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import {
4+
isError,
5+
isEvalError,
6+
isRangeError,
7+
isReferenceError,
8+
isSyntaxError,
9+
isTypeError,
10+
isURIError,
11+
} from "./built-in.js";
12+
13+
describe("isError(error)", () => {
14+
it(`🔙 returns a boolean 🟢 'true' - on Error`, () => {
15+
expect(isError(new Error("message"))).toBe(true);
16+
});
17+
18+
it(`🔙 returns a boolean 🔴 'false' - on random value`, () => {
19+
expect(isError("")).toBe(false);
20+
});
21+
});
22+
23+
describe("isEvalError(error)", () => {
24+
it(`🔙 returns a boolean 🔴 'false' - on Error`, () => {
25+
expect(isRangeError(Error)).toBe(false);
26+
});
27+
28+
it(`🔙 returns a boolean 🔴 'false' - on random value`, () => {
29+
expect(isEvalError("")).toBe(false);
30+
});
31+
32+
it(`🔙 returns a boolean 🟢 'true' - on EvalError`, () => {
33+
expect(isEvalError(new EvalError("message"))).toBe(true);
34+
});
35+
});
36+
37+
describe("isRangeError(error)", () => {
38+
it(`🔙 returns a boolean 🔴 'false' - on Error`, () => {
39+
expect(isRangeError(Error)).toBe(false);
40+
});
41+
42+
it(`🔙 returns a boolean 🔴 'false' - on random value`, () => {
43+
expect(isRangeError("")).toBe(false);
44+
});
45+
46+
it(`🔙 returns a boolean 🟢 'true' - on RangeError`, () => {
47+
expect(isRangeError(new RangeError("message"))).toBe(true);
48+
});
49+
});
50+
51+
describe("isReferenceError(error)", () => {
52+
it(`🔙 returns a boolean 🔴 'false' - on Error`, () => {
53+
expect(isRangeError(Error)).toBe(false);
54+
});
55+
56+
it(`🔙 returns a boolean 🔴 'false' - on random value`, () => {
57+
expect(isReferenceError("")).toBe(false);
58+
});
59+
60+
it(`🔙 returns a boolean 🟢 'true' - on ReferenceError`, () => {
61+
expect(isReferenceError(new ReferenceError("message"))).toBe(true);
62+
});
63+
});
64+
65+
describe("isSyntaxError(error)", () => {
66+
it(`🔙 returns a boolean 🔴 'false' - on Error`, () => {
67+
expect(isRangeError(Error)).toBe(false);
68+
});
69+
70+
it(`🔙 returns a boolean 🔴 'false' - on random value`, () => {
71+
expect(isSyntaxError("")).toBe(false);
72+
});
73+
74+
it(`🔙 returns a boolean 🟢 'true' - on SyntaxError`, () => {
75+
expect(isSyntaxError(new SyntaxError("message"))).toBe(true);
76+
});
77+
});
78+
79+
describe("isTypeError(error)", () => {
80+
it(`🔙 returns a boolean 🔴 'false' - on Error`, () => {
81+
expect(isRangeError(Error)).toBe(false);
82+
});
83+
84+
it(`🔙 returns a boolean 🔴 'false' - on random value`, () => {
85+
expect(isTypeError("")).toBe(false);
86+
});
87+
88+
it(`🔙 returns a boolean 🟢 'true' - on TypeError`, () => {
89+
expect(isTypeError(new TypeError("message"))).toBe(true);
90+
});
91+
});
92+
93+
describe("isURIError(error)", () => {
94+
it(`🔙 returns a boolean 🔴 'false' - on Error`, () => {
95+
expect(isRangeError(Error)).toBe(false);
96+
});
97+
98+
it(`🔙 returns a boolean 🔴 'false' - on random value`, () => {
99+
expect(isURIError("")).toBe(false);
100+
});
101+
102+
it(`🔙 returns a boolean 🟢 'true' - on URIError`, () => {
103+
expect(isURIError(new URIError("message"))).toBe(true);
104+
});
105+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { z } from "zod";
2+
3+
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error} */
4+
export const ERROR_SCHEMA = z.instanceof(Error);
5+
/** @see {@link ERROR_SCHEMA} */
6+
export function isError(error: unknown): error is Error {
7+
return ERROR_SCHEMA.safeParse(error).success;
8+
}
9+
10+
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError} */
11+
export const EVAL_ERROR_SCHEMA = z.instanceof(EvalError);
12+
/** @see {@link EVAL_ERROR_SCHEMA} */
13+
export function isEvalError(error: unknown): error is EvalError {
14+
return EVAL_ERROR_SCHEMA.safeParse(error).success;
15+
}
16+
17+
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError} */
18+
export const RANGE_ERROR_SCHEMA = z.instanceof(RangeError);
19+
/** @see {@link RANGE_ERROR_SCHEMA} */
20+
export function isRangeError(error: unknown): error is RangeError {
21+
return RANGE_ERROR_SCHEMA.safeParse(error).success;
22+
}
23+
24+
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError} */
25+
export const REFERENCE_ERROR_SCHEMA = z.instanceof(ReferenceError);
26+
/** @see {@link REFERENCE_ERROR_SCHEMA} */
27+
export function isReferenceError(error: unknown): error is ReferenceError {
28+
return REFERENCE_ERROR_SCHEMA.safeParse(error).success;
29+
}
30+
31+
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError} */
32+
export const SYNTAX_ERROR_SCHEMA = z.instanceof(SyntaxError);
33+
/** @see {@link SYNTAX_ERROR_SCHEMA} */
34+
export function isSyntaxError(error: unknown): error is SyntaxError {
35+
return SYNTAX_ERROR_SCHEMA.safeParse(error).success;
36+
}
37+
38+
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError} */
39+
export const TYPE_ERROR_SCHEMA = z.instanceof(TypeError);
40+
/** @see {@link TYPE_ERROR_SCHEMA} */
41+
export function isTypeError(error: unknown): error is TypeError {
42+
return TYPE_ERROR_SCHEMA.safeParse(error).success;
43+
}
44+
45+
/** @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/URIError} */
46+
export const URI_ERROR_SCHEMA = z.instanceof(URIError);
47+
/** @see {@link URI_ERROR_SCHEMA} */
48+
export function isURIError(error: unknown): error is URIError {
49+
return URI_ERROR_SCHEMA.safeParse(error).success;
50+
}
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
import { describe, expect, it } from "vitest";
22

3-
import { isError, isRuntimeError, isValidationError, RuntimeError, ValidationError } from "./custom.js";
3+
import { isRuntimeError, isValidationError, RuntimeError, ValidationError } from "./custom.js";
44

5-
describe("isError(error)", () => {
6-
it(`🔙 returns a boolean 🟢 'true' - on Error`, () => {
7-
expect(isError(new Error("message"))).toBe(true);
5+
describe("isRuntimeError(error)", () => {
6+
it(`🔙 returns a boolean 🔴 'false' - on Error`, () => {
7+
expect(isRuntimeError(Error)).toBe(false);
88
});
99

1010
it(`🔙 returns a boolean 🔴 'false' - on random value`, () => {
11-
expect(isError("")).toBe(false);
11+
expect(isRuntimeError("")).toBe(false);
1212
});
13-
});
1413

15-
describe("isRuntimeError(error)", () => {
1614
it(`🔙 returns a boolean 🟢 'true' - on RuntimeError`, () => {
1715
expect(isRuntimeError(new RuntimeError("message"))).toBe(true);
1816
});
17+
});
1918

19+
describe("isValidationError(error)", () => {
2020
it(`🔙 returns a boolean 🔴 'false' - on Error`, () => {
21-
expect(isRuntimeError(new Error("message"))).toBe(false);
21+
expect(isValidationError(Error)).toBe(false);
2222
});
23-
});
2423

25-
describe("isValidationError(error)", () => {
26-
it(`🔙 returns a boolean 🟢 'true' - on ValidationError`, () => {
27-
expect(isValidationError(new ValidationError([]))).toBe(true);
24+
it(`🔙 returns a boolean 🔴 'false' - on random value`, () => {
25+
expect(isValidationError("")).toBe(false);
2826
});
2927

30-
it(`🔙 returns a boolean 🔴 'false' - on Error`, () => {
31-
expect(isValidationError(new Error("message"))).toBe(false);
28+
it(`🔙 returns a boolean 🟢 'true' - on ValidationError`, () => {
29+
expect(isValidationError(new ValidationError([]))).toBe(true);
3230
});
3331
});

packages/error/source/custom/custom.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,29 @@
55
*/
66
import type {} from "modern-errors";
77
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-
}
8+
import { z, ZodError } from "zod";
139

10+
/** Error related to the currently used JavaScript runtime environment. */
1411
export const RuntimeError: typeof ModernError = ModernError.subclass("RuntimeError", {
1512
props: {
16-
isRuntimeError: true,
13+
bun: typeof globalThis.Bun,
14+
deno: typeof globalThis.Deno,
15+
process: typeof globalThis.process,
16+
window: typeof globalThis.window,
1717
},
1818
});
19-
19+
/** @see {@link RuntimeError} */
20+
export const RUNTIME_ERROR_SCHEMA: ReturnType<typeof z.instanceof<typeof RuntimeError>> = z.instanceof(RuntimeError);
21+
/** @see {@link RuntimeError} */
2022
export function isRuntimeError(error: unknown): error is typeof RuntimeError {
21-
return error instanceof RuntimeError;
23+
return RUNTIME_ERROR_SCHEMA.safeParse(error).success;
2224
}
2325

26+
/** Error related to the type validation. */
2427
export const ValidationError = ZodError;
25-
28+
/** @see {@link ValidationError} */
29+
export const VALIDATION_ERROR_SCHEMA = z.instanceof(ValidationError);
30+
/** @see {@link ValidationError} */
2631
export function isValidationError(error: unknown): error is typeof ValidationError {
27-
return error instanceof ValidationError;
32+
return VALIDATION_ERROR_SCHEMA.safeParse(error).success;
2833
}

packages/error/source/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/* MODULES */
2+
export * from "./built-in/built-in.js";
23
export * from "./custom/custom.js";

0 commit comments

Comments
 (0)