Skip to content

Commit 7d43404

Browse files
committed
feat: check helper for base error
1 parent f8aaed2 commit 7d43404

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+278
-204
lines changed

packages/ebec/src/check.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2026.
3+
* Author Peter Placzek (tada5hi)
4+
* For the full copyright and license information,
5+
* view the LICENSE file that was distributed with this source code.
6+
*/
7+
import { isOptions } from './options';
8+
import { isObject } from './utils';
9+
import { BaseError } from './module';
10+
11+
export function isBaseError(
12+
input: unknown,
13+
): input is BaseError {
14+
if (!isObject(input)) {
15+
return false;
16+
}
17+
18+
if (input instanceof BaseError) {
19+
return true;
20+
}
21+
22+
if (!isOptions(input)) {
23+
return false;
24+
}
25+
26+
if (!isObject(input.data)) {
27+
return false;
28+
}
29+
30+
return typeof input.message === 'string';
31+
}

packages/ebec/src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*
2+
* Copyright (c) 2026.
3+
* Author Peter Placzek (tada5hi)
4+
* For the full copyright and license information,
5+
* view the LICENSE file that was distributed with this source code.
6+
*/
7+
18
export * from './types';
9+
export * from './check';
210
export * from './module';
3-
export * from './helpers';
11+
export * from './options';
12+
export * from './utils';

packages/ebec/src/module.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
1-
import type { Input } from './types';
1+
/*
2+
* Copyright (c) 2026.
3+
* Author Peter Placzek (tada5hi)
4+
* For the full copyright and license information,
5+
* view the LICENSE file that was distributed with this source code.
6+
*/
7+
8+
import type { ErrorInput, ObjectLiteral } from './types';
29
import {
310
extractOptions,
4-
isOptions,
5-
} from './helpers';
11+
} from './options';
612

713
export class BaseError extends Error {
814
/**
915
* A unique identifier for the error,
1016
* which can be a short uppercase string or a numeric code.
1117
*/
12-
code?: string | number | null;
18+
readonly code?: string | number | null;
1319

1420
/**
1521
* Additional data associated with the error. This property can hold
1622
* unstructured information or supplementary details that provide context
1723
* to the error.
1824
*/
19-
data?: unknown;
25+
readonly data: ObjectLiteral;
2026

2127
/**
2228
* Determines whether the error message can be safely exposed externally.
2329
*/
24-
expose?: boolean;
30+
readonly expose?: boolean;
2531

2632
/**
2733
* Indicates whether the error should be logged in the application's logs.
2834
*/
29-
logMessage?: boolean;
35+
readonly logMessage?: boolean;
3036

3137
/**
3238
* Specifies the log level at which this error should be recorded.
3339
*/
34-
logLevel?: string | number;
40+
readonly logLevel?: string | number;
3541

3642
/**
3743
* Represents the underlying cause or source of the error.
@@ -40,7 +46,7 @@ export class BaseError extends Error {
4046

4147
//--------------------------------------------------------------------
4248

43-
constructor(...input: Input[]) {
49+
constructor(...input: ErrorInput[]) {
4450
const options = extractOptions(...input);
4551

4652
super(options.message, { cause: options.cause });
@@ -67,16 +73,6 @@ export class BaseError extends Error {
6773
this.expose = options.expose;
6874
this.logMessage = options.logMessage;
6975
this.logLevel = options.logLevel;
70-
this.data = options.data;
76+
this.data = options.data || {};
7177
}
7278
}
73-
74-
export function isBaseError(
75-
input: unknown,
76-
): input is BaseError {
77-
if (!isOptions(input)) {
78-
return false;
79-
}
80-
81-
return typeof input.message === 'string';
82-
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* view the LICENSE file that was distributed with this source code.
66
*/
77

8-
import { isObject } from './object';
8+
import { isObject } from '../utils/object';
99

1010
export function isError(input: unknown) : input is Error & { [key: string]: any } {
1111
if (!isObject(input)) {

packages/ebec/src/options/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright (c) 2026.
3+
* Author Peter Placzek (tada5hi)
4+
* For the full copyright and license information,
5+
* view the LICENSE file that was distributed with this source code.
6+
*/
7+
8+
export * from './check';
9+
export * from './module';
10+
export * from './types';
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77

88
import {
99
isObject,
10-
} from './object';
11-
import type { Input, Options } from '../types';
10+
} from '../utils';
11+
import type { ErrorInput } from '../types';
1212
import { isError } from './check';
13+
import type { Options } from './types';
1314

1415
type CheckFn<T> = (input: unknown) => input is T;
1516
export function createExtractOptionsFn<T extends Options>(fn: CheckFn<T>) {
16-
return (...input: Input[]) : T => {
17+
return (...input: ErrorInput[]) : T => {
1718
const output : T = {} as T;
1819
for (let i = 0; i < input.length; i++) {
1920
const element = input[i];
@@ -92,6 +93,6 @@ export function isOptions(input: unknown) : input is Options {
9293
}
9394

9495
const check = createExtractOptionsFn(isOptions);
95-
export function extractOptions(...input: Input[]) {
96+
export function extractOptions(...input: ErrorInput[]) {
9697
return check(...input);
9798
}

packages/ebec/src/options/types.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2026.
3+
* Author Peter Placzek (tada5hi)
4+
* For the full copyright and license information,
5+
* view the LICENSE file that was distributed with this source code.
6+
*/
7+
import type { ObjectLiteral } from '../types';
8+
9+
export type Options = {
10+
/**
11+
* The actual error message, if not provided on another way.
12+
*/
13+
message?: string,
14+
15+
/**
16+
* Trace of which functions were called.
17+
*/
18+
stack?: string
19+
20+
/**
21+
* A unique identifier for the error,
22+
* which can be a short uppercase string or a numeric code.
23+
*/
24+
code?: string | number | null,
25+
26+
/**
27+
* Additional data associated with the error. This property can hold
28+
* unstructured information or supplementary details that provide context
29+
* to the error.
30+
*/
31+
data?: ObjectLiteral,
32+
33+
/**
34+
* Determines whether the error message can be safely exposed externally.
35+
*/
36+
expose?: boolean;
37+
38+
/**
39+
* Indicates whether the error should be logged in the application's logs.
40+
*/
41+
logMessage?: boolean,
42+
43+
/**
44+
* Specifies the log level at which this error should be recorded.
45+
*/
46+
logLevel?: string | number,
47+
48+
/**
49+
* Represents the underlying cause or source of the error.
50+
*/
51+
cause?: unknown
52+
};

packages/ebec/src/types.ts

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,12 @@
1-
export type Options = {
2-
/**
3-
* The actual error message, if not provided on another way.
4-
*/
5-
message?: string,
1+
/*
2+
* Copyright (c) 2026.
3+
* Author Peter Placzek (tada5hi)
4+
* For the full copyright and license information,
5+
* view the LICENSE file that was distributed with this source code.
6+
*/
67

7-
/**
8-
* Trace of which functions were called.
9-
*/
10-
stack?: string
8+
import type { Options } from './options';
119

12-
/**
13-
* A unique identifier for the error,
14-
* which can be a short uppercase string or a numeric code.
15-
*/
16-
code?: string | number | null,
10+
export type ObjectLiteral = Record<PropertyKey, any>;
1711

18-
/**
19-
* Additional data associated with the error. This property can hold
20-
* unstructured information or supplementary details that provide context
21-
* to the error.
22-
*/
23-
data?: unknown,
24-
25-
/**
26-
* Determines whether the error message can be safely exposed externally.
27-
*/
28-
expose?: boolean;
29-
30-
/**
31-
* Indicates whether the error should be logged in the application's logs.
32-
*/
33-
logMessage?: boolean,
34-
35-
/**
36-
* Specifies the log level at which this error should be recorded.
37-
*/
38-
logLevel?: string | number,
39-
40-
/**
41-
* Represents the underlying cause or source of the error.
42-
*/
43-
cause?: unknown
44-
};
45-
46-
export type Input = Options | Error | string;
12+
export type ErrorInput = Options | Error | string;
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@
55
* view the LICENSE file that was distributed with this source code.
66
*/
77

8-
export * from './check';
98
export * from './object';
10-
export * from './options';

0 commit comments

Comments
 (0)