@wrelik/errors 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @wrelik/errors
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - ad849e3: Initial release of all wrelik-kit packages.
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@wrelik/errors",
3
+ "version": "0.1.0",
4
+ "main": "./dist/index.js",
5
+ "types": "./dist/index.d.ts",
6
+ "dependencies": {
7
+ "@sentry/node": "^7.101.1"
8
+ },
9
+ "devDependencies": {
10
+ "tsup": "^8.0.1",
11
+ "vitest": "^1.2.2",
12
+ "@wrelik/eslint-config": "0.1.0",
13
+ "@wrelik/tsconfig": "0.1.0"
14
+ },
15
+ "scripts": {
16
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
17
+ "lint": "eslint src/",
18
+ "test": "vitest run --passWithNoTests"
19
+ }
20
+ }
package/src/index.ts ADDED
@@ -0,0 +1,70 @@
1
+ import * as Sentry from '@sentry/node';
2
+
3
+ export class AppError extends Error {
4
+ public readonly code: string;
5
+ public readonly statusCode: number;
6
+ public readonly context?: Record<string, any>;
7
+
8
+ constructor(
9
+ message: string,
10
+ code: string,
11
+ statusCode: number = 500,
12
+ context?: Record<string, any>,
13
+ ) {
14
+ super(message);
15
+ this.name = 'AppError';
16
+ this.code = code;
17
+ this.statusCode = statusCode;
18
+ this.context = context;
19
+ }
20
+ }
21
+
22
+ export class AuthRequiredError extends AppError {
23
+ constructor(message = 'Authentication required', context?: Record<string, any>) {
24
+ super(message, 'AUTH_REQUIRED', 401, context);
25
+ }
26
+ }
27
+
28
+ export class TenantRequiredError extends AppError {
29
+ constructor(message = 'Tenant context required', context?: Record<string, any>) {
30
+ super(message, 'TENANT_REQUIRED', 400, context);
31
+ }
32
+ }
33
+
34
+ export class PermissionDeniedError extends AppError {
35
+ constructor(message = 'Permission denied', context?: Record<string, any>) {
36
+ super(message, 'PERMISSION_DENIED', 403, context);
37
+ }
38
+ }
39
+
40
+ export class ValidationError extends AppError {
41
+ constructor(message: string, context?: Record<string, any>) {
42
+ super(message, 'VALIDATION_ERROR', 400, context);
43
+ }
44
+ }
45
+
46
+ export function captureError(err: unknown, context?: Record<string, any>) {
47
+ if (err instanceof AppError) {
48
+ // Basic logging for expected app errors
49
+ console.warn(`[${err.code}] ${err.message}`, { context: err.context, ...context });
50
+ // Still capture if it's 500 or critical? Maybe optional.
51
+ if (err.statusCode >= 500) {
52
+ Sentry.captureException(err, { extra: { ...err.context, ...context } });
53
+ }
54
+ } else {
55
+ // Unexpected errors
56
+ console.error('Unexpected error:', err);
57
+ Sentry.captureException(err, { extra: context });
58
+ }
59
+ }
60
+
61
+ export function setUserContext(user: { id: string; email?: string } | null) {
62
+ Sentry.setUser(user);
63
+ }
64
+
65
+ export function initErrors(dsn: string) {
66
+ Sentry.init({
67
+ dsn,
68
+ tracesSampleRate: 1.0,
69
+ });
70
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "@wrelik/tsconfig/node.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist"
5
+ },
6
+ "include": ["src"]
7
+ }