@donotdev/functions 0.0.13 → 0.0.15
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@donotdev/functions",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.15",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Backend functions for DoNotDev Framework - Firebase, Vercel, and platform-agnostic implementations for auth, billing, CRUD, and OAuth",
|
|
6
6
|
"main": "./lib/firebase/index.js",
|
|
@@ -47,13 +47,13 @@
|
|
|
47
47
|
"serve": "firebase emulators:start --only functions"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@donotdev/core": "^0.0.
|
|
51
|
-
"@donotdev/firebase": "^0.0.
|
|
52
|
-
"@donotdev/supabase": "^0.0.
|
|
50
|
+
"@donotdev/core": "^0.0.28",
|
|
51
|
+
"@donotdev/firebase": "^0.0.15",
|
|
52
|
+
"@donotdev/supabase": "^0.0.5"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"@sentry/node": "^10.39.0",
|
|
56
|
-
"@supabase/supabase-js": "^2.76.
|
|
56
|
+
"@supabase/supabase-js": "^2.76.14",
|
|
57
57
|
"firebase-admin": "^13.6.1",
|
|
58
58
|
"firebase-functions": "^7.0.5",
|
|
59
59
|
"next": "^16.1.6",
|
|
@@ -18,7 +18,7 @@ import type { UserRole } from '@donotdev/core/server';
|
|
|
18
18
|
import type { SecurityContext } from '@donotdev/core';
|
|
19
19
|
|
|
20
20
|
import { FUNCTION_CONFIG } from './config/constants.js';
|
|
21
|
-
import { handleError } from '../shared/errorHandling.js';
|
|
21
|
+
import { handleError, DoNotDevError } from '../shared/errorHandling.js';
|
|
22
22
|
import { assertAuthenticated, getUserRole } from '../shared/utils.js';
|
|
23
23
|
|
|
24
24
|
import type {
|
|
@@ -321,7 +321,19 @@ export function createBaseFunction<TRequest, TResponse>(
|
|
|
321
321
|
});
|
|
322
322
|
}
|
|
323
323
|
|
|
324
|
-
|
|
324
|
+
try {
|
|
325
|
+
handleError(error);
|
|
326
|
+
} catch (classified) {
|
|
327
|
+
if (classified instanceof DoNotDevError) {
|
|
328
|
+
const { HttpsError } = await import('firebase-functions/v2/https');
|
|
329
|
+
throw new HttpsError(
|
|
330
|
+
classified.code as any,
|
|
331
|
+
classified.message,
|
|
332
|
+
classified.details
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
throw classified;
|
|
336
|
+
}
|
|
325
337
|
}
|
|
326
338
|
}
|
|
327
339
|
);
|
|
@@ -1,41 +1,19 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach
|
|
2
|
-
|
|
3
|
-
// Mock firebase-functions
|
|
4
|
-
vi.mock('firebase-functions/v2/https', () => ({
|
|
5
|
-
HttpsError: class HttpsError extends Error {
|
|
6
|
-
code: string;
|
|
7
|
-
details: any;
|
|
8
|
-
constructor(code: string, message: string, details?: any) {
|
|
9
|
-
super(message);
|
|
10
|
-
this.code = code;
|
|
11
|
-
this.details = details;
|
|
12
|
-
this.name = 'HttpsError';
|
|
13
|
-
}
|
|
14
|
-
},
|
|
15
|
-
}));
|
|
16
|
-
|
|
17
|
-
vi.mock('firebase-functions/v2', () => ({
|
|
18
|
-
logger: { warn: vi.fn(), info: vi.fn(), error: vi.fn(), log: vi.fn() },
|
|
19
|
-
}));
|
|
20
|
-
|
|
21
|
-
vi.mock('@donotdev/firebase/server', () => ({
|
|
22
|
-
getFirebaseAdminFirestore: vi.fn(),
|
|
23
|
-
}));
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
24
2
|
|
|
25
3
|
import { handleError, DoNotDevError } from '../errorHandling';
|
|
26
4
|
|
|
27
5
|
describe('DoNotDevError (functions)', () => {
|
|
28
|
-
it('creates with default code "
|
|
6
|
+
it('creates with default code "internal"', () => {
|
|
29
7
|
const error = new DoNotDevError('test');
|
|
30
8
|
|
|
31
|
-
expect(error.code).toBe('
|
|
9
|
+
expect(error.code).toBe('internal');
|
|
32
10
|
expect(error.message).toBe('test');
|
|
33
11
|
expect(error.name).toBe('DoNotDevError');
|
|
34
12
|
});
|
|
35
13
|
|
|
36
14
|
it('creates with explicit code and details', () => {
|
|
37
15
|
const error = new DoNotDevError('bad input', 'invalid-argument', {
|
|
38
|
-
field: 'email',
|
|
16
|
+
details: { field: 'email' },
|
|
39
17
|
});
|
|
40
18
|
|
|
41
19
|
expect(error.code).toBe('invalid-argument');
|
|
@@ -48,18 +26,6 @@ describe('DoNotDevError (functions)', () => {
|
|
|
48
26
|
expect(error.toString()).toBe('DoNotDevError [not-found]: test');
|
|
49
27
|
});
|
|
50
28
|
|
|
51
|
-
it('toJSON includes all fields', () => {
|
|
52
|
-
const error = new DoNotDevError('test', 'internal', { key: 'val' });
|
|
53
|
-
const json = error.toJSON();
|
|
54
|
-
|
|
55
|
-
expect(json).toEqual({
|
|
56
|
-
name: 'DoNotDevError',
|
|
57
|
-
code: 'internal',
|
|
58
|
-
message: 'test',
|
|
59
|
-
details: { key: 'val' },
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
|
|
63
29
|
it('instanceof chain works', () => {
|
|
64
30
|
const error = new DoNotDevError('test');
|
|
65
31
|
|
|
@@ -69,54 +35,62 @@ describe('DoNotDevError (functions)', () => {
|
|
|
69
35
|
});
|
|
70
36
|
|
|
71
37
|
describe('handleError', () => {
|
|
72
|
-
const originalEnv = { ...process.env };
|
|
73
|
-
|
|
74
38
|
beforeEach(() => {
|
|
75
39
|
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
76
|
-
// Clear env to force fallback (no Firebase, no Vercel)
|
|
77
|
-
delete process.env.FUNCTIONS_EMULATOR;
|
|
78
|
-
delete process.env.FIREBASE_CONFIG;
|
|
79
|
-
delete process.env.GCLOUD_PROJECT;
|
|
80
|
-
delete process.env.VERCEL;
|
|
81
|
-
delete process.env.VERCEL_ENV;
|
|
82
40
|
});
|
|
83
41
|
|
|
84
|
-
|
|
85
|
-
process.env = { ...originalEnv };
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it('throws for DoNotDevError (fallback env)', () => {
|
|
42
|
+
it('throws DoNotDevError for DoNotDevError input', () => {
|
|
89
43
|
const original = new DoNotDevError('Bad', 'invalid-argument');
|
|
90
44
|
|
|
91
|
-
|
|
45
|
+
try {
|
|
46
|
+
handleError(original);
|
|
47
|
+
expect.fail('should have thrown');
|
|
48
|
+
} catch (error) {
|
|
49
|
+
expect(error).toBeInstanceOf(DoNotDevError);
|
|
50
|
+
expect((error as DoNotDevError).code).toBe('invalid-argument');
|
|
51
|
+
expect((error as DoNotDevError).message).toBe('Bad');
|
|
52
|
+
}
|
|
92
53
|
});
|
|
93
54
|
|
|
94
|
-
it('
|
|
95
|
-
const original = new
|
|
55
|
+
it('preserves details from DoNotDevError input', () => {
|
|
56
|
+
const original = new DoNotDevError('Bad', 'invalid-argument', {
|
|
57
|
+
details: { field: 'email' },
|
|
58
|
+
});
|
|
96
59
|
|
|
97
|
-
|
|
60
|
+
try {
|
|
61
|
+
handleError(original);
|
|
62
|
+
expect.fail('should have thrown');
|
|
63
|
+
} catch (error) {
|
|
64
|
+
expect(error).toBeInstanceOf(DoNotDevError);
|
|
65
|
+
expect((error as DoNotDevError).details).toEqual({ field: 'email' });
|
|
66
|
+
}
|
|
98
67
|
});
|
|
99
68
|
|
|
100
|
-
it('throws
|
|
101
|
-
|
|
102
|
-
'
|
|
103
|
-
|
|
69
|
+
it('throws DoNotDevError with code "internal" for generic Error', () => {
|
|
70
|
+
try {
|
|
71
|
+
handleError(new Error('Something broke'));
|
|
72
|
+
expect.fail('should have thrown');
|
|
73
|
+
} catch (error) {
|
|
74
|
+
expect(error).toBeInstanceOf(DoNotDevError);
|
|
75
|
+
expect((error as DoNotDevError).code).toBe('internal');
|
|
76
|
+
expect((error as DoNotDevError).message).toBe('Something broke');
|
|
77
|
+
}
|
|
104
78
|
});
|
|
105
79
|
|
|
106
|
-
it('throws
|
|
107
|
-
process.env.FIREBASE_CONFIG = '{}';
|
|
108
|
-
|
|
80
|
+
it('throws DoNotDevError with generic message for non-Error', () => {
|
|
109
81
|
try {
|
|
110
|
-
handleError(
|
|
82
|
+
handleError('random string');
|
|
111
83
|
expect.fail('should have thrown');
|
|
112
|
-
} catch (error
|
|
113
|
-
expect(error
|
|
114
|
-
expect(error.code).toBe('
|
|
84
|
+
} catch (error) {
|
|
85
|
+
expect(error).toBeInstanceOf(DoNotDevError);
|
|
86
|
+
expect((error as DoNotDevError).code).toBe('internal');
|
|
87
|
+
expect((error as DoNotDevError).message).toBe(
|
|
88
|
+
'An unexpected error occurred'
|
|
89
|
+
);
|
|
115
90
|
}
|
|
116
91
|
});
|
|
117
92
|
|
|
118
93
|
it('maps EntityHookError types correctly', () => {
|
|
119
|
-
// Simulate EntityHookError (name-based detection)
|
|
120
94
|
const entityError = new Error('Permission denied');
|
|
121
95
|
(entityError as any).name = 'EntityHookError';
|
|
122
96
|
(entityError as any).type = 'PERMISSION_DENIED';
|
|
@@ -124,12 +98,14 @@ describe('handleError', () => {
|
|
|
124
98
|
try {
|
|
125
99
|
handleError(entityError);
|
|
126
100
|
expect.fail('should have thrown');
|
|
127
|
-
} catch (error
|
|
128
|
-
expect(error
|
|
101
|
+
} catch (error) {
|
|
102
|
+
expect(error).toBeInstanceOf(DoNotDevError);
|
|
103
|
+
expect((error as DoNotDevError).code).toBe('permission-denied');
|
|
104
|
+
expect((error as DoNotDevError).message).toBe('Permission denied');
|
|
129
105
|
}
|
|
130
106
|
});
|
|
131
107
|
|
|
132
|
-
it('maps ValiError to invalid-argument', () => {
|
|
108
|
+
it('maps ValiError to invalid-argument with details', () => {
|
|
133
109
|
const valiError = new Error('Validation failed');
|
|
134
110
|
(valiError as any).name = 'ValiError';
|
|
135
111
|
(valiError as any).issues = [{ path: [], message: 'required' }];
|
|
@@ -137,8 +113,13 @@ describe('handleError', () => {
|
|
|
137
113
|
try {
|
|
138
114
|
handleError(valiError);
|
|
139
115
|
expect.fail('should have thrown');
|
|
140
|
-
} catch (error
|
|
141
|
-
expect(error
|
|
116
|
+
} catch (error) {
|
|
117
|
+
expect(error).toBeInstanceOf(DoNotDevError);
|
|
118
|
+
expect((error as DoNotDevError).code).toBe('invalid-argument');
|
|
119
|
+
expect((error as DoNotDevError).message).toBe('Validation failed');
|
|
120
|
+
expect((error as DoNotDevError).details).toEqual({
|
|
121
|
+
validationErrors: [{ path: [], message: 'required' }],
|
|
122
|
+
});
|
|
142
123
|
}
|
|
143
124
|
});
|
|
144
125
|
});
|
|
@@ -2,30 +2,31 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @fileoverview Shared Error Handling
|
|
5
|
-
* @description Platform-
|
|
5
|
+
* @description Platform-agnostic error classification. Always throws DoNotDevError.
|
|
6
|
+
* Platform adapters (Firebase baseFunction, Vercel route handler) catch and convert.
|
|
6
7
|
*
|
|
7
|
-
* @version 0.0.
|
|
8
|
+
* @version 0.0.2
|
|
8
9
|
* @since 0.0.1
|
|
9
10
|
* @author AMBROISE PARK Consulting
|
|
10
11
|
*/
|
|
11
12
|
|
|
12
|
-
import * as v from 'valibot';
|
|
13
|
-
|
|
14
13
|
import { DoNotDevError, EntityHookError } from '@donotdev/core/server';
|
|
15
14
|
|
|
16
15
|
// Re-export DoNotDevError so existing imports from this module continue to work
|
|
17
16
|
export { DoNotDevError };
|
|
18
17
|
|
|
19
18
|
/**
|
|
20
|
-
*
|
|
19
|
+
* Classify and throw a DoNotDevError from any unknown error.
|
|
20
|
+
* Pure classification — no platform imports, no env detection.
|
|
21
|
+
*
|
|
21
22
|
* @param error - The error to handle
|
|
22
|
-
* @throws
|
|
23
|
+
* @throws DoNotDevError always
|
|
23
24
|
*
|
|
24
|
-
* @version 0.0.
|
|
25
|
+
* @version 0.0.2
|
|
25
26
|
* @since 0.0.1
|
|
26
27
|
* @author AMBROISE PARK Consulting
|
|
27
28
|
*/
|
|
28
|
-
export
|
|
29
|
+
export function handleError(error: unknown): never {
|
|
29
30
|
// W11: Log only in development to avoid double-logging with platform loggers.
|
|
30
31
|
// Known DoNotDevError, ValiError, and EntityHookError are handled by callers.
|
|
31
32
|
if (
|
|
@@ -94,76 +95,9 @@ export async function handleError(error: unknown): Promise<never> {
|
|
|
94
95
|
details = undefined;
|
|
95
96
|
}
|
|
96
97
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
throwVercelError(code, message, details);
|
|
102
|
-
} else {
|
|
103
|
-
// Fallback - just throw a regular error
|
|
104
|
-
throw new Error(`${code}: ${message}`);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Detect if we're running in Firebase environment
|
|
110
|
-
*
|
|
111
|
-
* @version 0.0.1
|
|
112
|
-
* @since 0.0.1
|
|
113
|
-
* @author AMBROISE PARK Consulting
|
|
114
|
-
*/
|
|
115
|
-
function isFirebaseEnvironment(): boolean {
|
|
116
|
-
return (
|
|
117
|
-
typeof process !== 'undefined' &&
|
|
118
|
-
(process.env.FUNCTIONS_EMULATOR === 'true' ||
|
|
119
|
-
process.env.FIREBASE_CONFIG !== undefined ||
|
|
120
|
-
process.env.GCLOUD_PROJECT !== undefined)
|
|
98
|
+
throw new DoNotDevError(
|
|
99
|
+
message,
|
|
100
|
+
code as any,
|
|
101
|
+
details ? { details } : undefined
|
|
121
102
|
);
|
|
122
103
|
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Detect if we're running in Vercel environment
|
|
126
|
-
*
|
|
127
|
-
* @version 0.0.1
|
|
128
|
-
* @since 0.0.1
|
|
129
|
-
* @author AMBROISE PARK Consulting
|
|
130
|
-
*/
|
|
131
|
-
function isVercelEnvironment(): boolean {
|
|
132
|
-
return (
|
|
133
|
-
typeof process !== 'undefined' &&
|
|
134
|
-
(process.env.VERCEL === '1' || process.env.VERCEL_ENV !== undefined)
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Throw Firebase-specific error
|
|
140
|
-
*
|
|
141
|
-
* @version 0.0.1
|
|
142
|
-
* @since 0.0.1
|
|
143
|
-
* @author AMBROISE PARK Consulting
|
|
144
|
-
*/
|
|
145
|
-
async function throwFirebaseError(
|
|
146
|
-
code: string,
|
|
147
|
-
message: string,
|
|
148
|
-
details?: any
|
|
149
|
-
): Promise<never> {
|
|
150
|
-
// Dynamic import — firebase-functions is only loaded in Firebase environments
|
|
151
|
-
const { HttpsError } = await import('firebase-functions/v2/https');
|
|
152
|
-
throw new HttpsError(code as any, message, details);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Throw Vercel-specific error
|
|
157
|
-
*
|
|
158
|
-
* @version 0.0.1
|
|
159
|
-
* @since 0.0.1
|
|
160
|
-
* @author AMBROISE PARK Consulting
|
|
161
|
-
*/
|
|
162
|
-
function throwVercelError(code: string, message: string, details?: any): never {
|
|
163
|
-
// For Vercel, we can throw a regular Error and let the API route handle it
|
|
164
|
-
// Or we could create a custom VercelError class
|
|
165
|
-
const error = new Error(message);
|
|
166
|
-
(error as any).code = code;
|
|
167
|
-
(error as any).details = details;
|
|
168
|
-
throw error;
|
|
169
|
-
}
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
import * as v from 'valibot';
|
|
13
13
|
|
|
14
|
-
import { handleError } from '../shared/errorHandling.js';
|
|
14
|
+
import { handleError, DoNotDevError } from '../shared/errorHandling.js';
|
|
15
15
|
import { recordPaymentMetrics } from '../shared/utils/internal/monitoring.js';
|
|
16
16
|
import {
|
|
17
17
|
checkRateLimitWithFirestore,
|
|
@@ -126,44 +126,44 @@ export function createVercelBaseFunction<TRequest, TResponse>(
|
|
|
126
126
|
});
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
//
|
|
130
|
-
|
|
129
|
+
// Classify error via shared handler, then map to HTTP response
|
|
130
|
+
try {
|
|
131
|
+
handleError(error);
|
|
132
|
+
} catch (classified) {
|
|
133
|
+
const errorResponse = classifiedToHttp(classified);
|
|
134
|
+
return res
|
|
135
|
+
.status(errorResponse.status)
|
|
136
|
+
.json({ error: errorResponse.message }) as TResponse;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Unreachable — handleError always throws. Fallback for TypeScript.
|
|
131
140
|
return res
|
|
132
|
-
.status(
|
|
133
|
-
.json({ error:
|
|
141
|
+
.status(500)
|
|
142
|
+
.json({ error: 'Internal server error' }) as TResponse;
|
|
134
143
|
}
|
|
135
144
|
};
|
|
136
145
|
}
|
|
137
146
|
|
|
138
|
-
/**
|
|
139
|
-
|
|
140
|
-
*/
|
|
141
|
-
function handleVercelError(error: unknown): {
|
|
147
|
+
/** Map a DoNotDevError code to an HTTP status + message */
|
|
148
|
+
function classifiedToHttp(error: unknown): {
|
|
142
149
|
status: number;
|
|
143
150
|
message: string;
|
|
144
151
|
} {
|
|
145
|
-
if (error instanceof
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
if (error.message.includes('already-exists')) {
|
|
160
|
-
return { status: 409, message: 'Resource already exists' };
|
|
161
|
-
}
|
|
162
|
-
if (error.message.includes('invalid-argument')) {
|
|
163
|
-
return { status: 400, message: error.message };
|
|
164
|
-
}
|
|
152
|
+
if (error instanceof DoNotDevError) {
|
|
153
|
+
const STATUS_MAP: Record<string, number> = {
|
|
154
|
+
'invalid-argument': 400,
|
|
155
|
+
'validation-failed': 400,
|
|
156
|
+
unauthenticated: 401,
|
|
157
|
+
'permission-denied': 403,
|
|
158
|
+
'not-found': 404,
|
|
159
|
+
'already-exists': 409,
|
|
160
|
+
'rate-limit-exceeded': 429,
|
|
161
|
+
'deadline-exceeded': 504,
|
|
162
|
+
unavailable: 503,
|
|
163
|
+
};
|
|
164
|
+
const status = STATUS_MAP[error.code] ?? 500;
|
|
165
|
+
return { status, message: error.message };
|
|
165
166
|
}
|
|
166
167
|
|
|
167
|
-
// Default to internal server error
|
|
168
168
|
return { status: 500, message: 'Internal server error' };
|
|
169
169
|
}
|