Skip to content

Commit 972487f

Browse files
committed
errors
1 parent 1be82a6 commit 972487f

File tree

10 files changed

+95
-87
lines changed

10 files changed

+95
-87
lines changed

REPO_NOTES.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
⚠️ Due to the managing of many versions, we do have some duplication, please beware!
2+
3+
There is a templates/ dir to solve some of this.
4+
5+
## Code Duplication 📋
6+
7+
### 1. Identical Test Files
8+
- All `versions/*/test/errors.test.js` files are identical (324 lines each)
9+
- All `versions/*/test/parsing.test.js` files are identical (89 lines each)
10+
- **Recommendation**: Consider using the template approach mentioned by the user
11+
12+
### 2. Nearly Identical Source Files
13+
- `versions/*/src/index.ts` are nearly identical except for version numbers
14+
- `versions/*/src/wasm_wrapper.c` are identical
15+
- `versions/*/Makefile` differ only in:
16+
- `LIBPG_QUERY_TAG` version
17+
- Version 13 has an extra emscripten patch
18+
19+
## Consistency Issues 🔧
20+
21+
### 1. Version 13 Makefile Difference
22+
- Version 13 applies an extra patch: `emscripten_disable_spinlocks.patch`
23+
- Other versions don't have this patch
24+
- **Status**: Patch file exists and is likely needed for v13 compatibility

full/src/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ function ptrToString(ptr: number): string {
187187
export const parse = awaitInit(async (query: string): Promise<ParseResult> => {
188188
// Input validation
189189
if (query === null || query === undefined) {
190-
throw new SqlError('Query cannot be null or undefined');
190+
throw new Error('Query cannot be null or undefined');
191191
}
192192

193193
if (query === '') {
194-
throw new SqlError('Query cannot be empty');
194+
throw new Error('Query cannot be empty');
195195
}
196196

197197
const queryPtr = stringToPtr(query);
@@ -200,7 +200,7 @@ export const parse = awaitInit(async (query: string): Promise<ParseResult> => {
200200
try {
201201
resultPtr = wasmModule._wasm_parse_query_raw(queryPtr);
202202
if (!resultPtr) {
203-
throw new SqlError('Failed to parse query: memory allocation failed');
203+
throw new Error('Failed to parse query: memory allocation failed');
204204
}
205205

206206
// Read the PgQueryParseResult struct
@@ -230,7 +230,7 @@ export const parse = awaitInit(async (query: string): Promise<ParseResult> => {
230230
}
231231

232232
if (!parseTreePtr) {
233-
throw new SqlError('No parse tree generated');
233+
throw new Error('No parse tree generated');
234234
}
235235

236236
const parseTreeStr = wasmModule.UTF8ToString(parseTreePtr);
@@ -343,11 +343,11 @@ export function parseSync(query: string): ParseResult {
343343

344344
// Input validation
345345
if (query === null || query === undefined) {
346-
throw new SqlError('Query cannot be null or undefined');
346+
throw new Error('Query cannot be null or undefined');
347347
}
348348

349349
if (query === '') {
350-
throw new SqlError('Query cannot be empty');
350+
throw new Error('Query cannot be empty');
351351
}
352352

353353
const queryPtr = stringToPtr(query);
@@ -356,7 +356,7 @@ export function parseSync(query: string): ParseResult {
356356
try {
357357
resultPtr = wasmModule._wasm_parse_query_raw(queryPtr);
358358
if (!resultPtr) {
359-
throw new SqlError('Failed to parse query: memory allocation failed');
359+
throw new Error('Failed to parse query: memory allocation failed');
360360
}
361361

362362
// Read the PgQueryParseResult struct
@@ -386,7 +386,7 @@ export function parseSync(query: string): ParseResult {
386386
}
387387

388388
if (!parseTreePtr) {
389-
throw new SqlError('No parse tree generated');
389+
throw new Error('No parse tree generated');
390390
}
391391

392392
const parseTreeStr = wasmModule.UTF8ToString(parseTreePtr);

parser/README.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ Multi-version PostgreSQL parser with dynamic version selection. This package pro
2121
# Install latest (full build with all versions)
2222
npm install @pgsql/parser
2323

24-
# Install LTS version (PostgreSQL 16-17 only)
24+
# Install LTS version (PostgreSQL 15-17 only)
2525
npm install @pgsql/parser@lts
26-
27-
# Install legacy version (PostgreSQL 13-15 only)
28-
npm install @pgsql/parser@legacy
2926
```
3027

3128
## Usage
@@ -116,15 +113,12 @@ Each version export provides:
116113

117114
This package supports different build configurations for different use cases:
118115

119-
- **full** (default): All versions (13, 14, 15, 16, 17)
120-
- **lts**: LTS versions only (16, 17)
121-
- **latest**: Latest version only (17)
122-
- **legacy**: Legacy versions (13, 14, 15)
116+
- **full** (default): All versions (13, 14, 15, 16, 17) - Provides maximum compatibility
117+
- **lts**: LTS (Long Term Support) versions only (15, 16, 17) - Recommended for production use with stable PostgreSQL versions
123118

124119
When installing from npm, you can choose the appropriate build using tags:
125-
- `npm install @pgsql/parser` - Full build
126-
- `npm install @pgsql/parser@lts` - LTS build
127-
- `npm install @pgsql/parser@legacy` - Legacy build
120+
- `npm install @pgsql/parser` - Full build with all versions
121+
- `npm install @pgsql/parser@lts` - LTS build
128122

129123
## Credits
130124

parser/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@
4747
"build": "npm run clean && npm run prepare",
4848
"build:full": "npm run clean && cross-env PARSER_BUILD_TYPE=full npm run prepare",
4949
"build:lts": "npm run clean && cross-env PARSER_BUILD_TYPE=lts npm run prepare",
50-
"build:latest": "npm run clean && cross-env PARSER_BUILD_TYPE=latest npm run prepare",
51-
"build:legacy": "npm run clean && cross-env PARSER_BUILD_TYPE=legacy npm run prepare",
5250
"test": "node --test test/parsing.test.js test/errors.test.js"
5351
},
5452
"keywords": [

parser/scripts/prepare.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ const BUILD_CONFIGS = {
99
description: 'Full build with all PostgreSQL versions (13-17)'
1010
},
1111
'lts': {
12-
versions: ['15', '16', '17'], // Current LTS versions
13-
description: 'LTS build with PostgreSQL 16 and 17'
14-
},
15-
'legacy': {
16-
versions: ['13', '14', '15'],
17-
description: 'Legacy versions (13-15)'
12+
versions: ['15', '16', '17'],
13+
description: 'LTS (Long Term Support)'
1814
}
1915
};
2016

versions/13/src/index.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,17 @@ export interface SqlErrorFormatOptions {
3030
maxQueryLength?: number; // Max query length to display (default: no limit)
3131
}
3232

33-
// Helper function to create enhanced error with SQL details
34-
function createSqlError(message: string, details: SqlErrorDetails): Error {
35-
const error = new Error(message);
36-
// Attach error details as properties
37-
Object.defineProperty(error, 'sqlDetails', {
38-
value: details,
39-
enumerable: true,
40-
configurable: true
41-
});
42-
return error;
33+
export class SqlError extends Error {
34+
sqlDetails?: SqlErrorDetails;
35+
36+
constructor(message: string, details?: SqlErrorDetails) {
37+
super(message);
38+
this.name = 'SqlError';
39+
this.sqlDetails = details;
40+
}
4341
}
4442

43+
4544
// Helper function to classify error source
4645
function getErrorSource(filename: string | null): string {
4746
if (!filename) return 'unknown';
@@ -227,7 +226,7 @@ export const parse = awaitInit(async (query: string) => {
227226
context: contextPtr ? wasmModule.UTF8ToString(contextPtr) : undefined
228227
};
229228

230-
throw createSqlError(message, errorDetails);
229+
throw new SqlError(message, errorDetails);
231230
}
232231

233232
if (!parseTreePtr) {
@@ -296,7 +295,7 @@ export function parseSync(query: string) {
296295
context: contextPtr ? wasmModule.UTF8ToString(contextPtr) : undefined
297296
};
298297

299-
throw createSqlError(message, errorDetails);
298+
throw new SqlError(message, errorDetails);
300299
}
301300

302301
if (!parseTreePtr) {

versions/14/src/index.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,17 @@ export interface SqlErrorFormatOptions {
3030
maxQueryLength?: number; // Max query length to display (default: no limit)
3131
}
3232

33-
// Helper function to create enhanced error with SQL details
34-
function createSqlError(message: string, details: SqlErrorDetails): Error {
35-
const error = new Error(message);
36-
// Attach error details as properties
37-
Object.defineProperty(error, 'sqlDetails', {
38-
value: details,
39-
enumerable: true,
40-
configurable: true
41-
});
42-
return error;
33+
export class SqlError extends Error {
34+
sqlDetails?: SqlErrorDetails;
35+
36+
constructor(message: string, details?: SqlErrorDetails) {
37+
super(message);
38+
this.name = 'SqlError';
39+
this.sqlDetails = details;
40+
}
4341
}
4442

43+
4544
// Helper function to classify error source
4645
function getErrorSource(filename: string | null): string {
4746
if (!filename) return 'unknown';
@@ -227,7 +226,7 @@ export const parse = awaitInit(async (query: string) => {
227226
context: contextPtr ? wasmModule.UTF8ToString(contextPtr) : undefined
228227
};
229228

230-
throw createSqlError(message, errorDetails);
229+
throw new SqlError(message, errorDetails);
231230
}
232231

233232
if (!parseTreePtr) {
@@ -296,7 +295,7 @@ export function parseSync(query: string) {
296295
context: contextPtr ? wasmModule.UTF8ToString(contextPtr) : undefined
297296
};
298297

299-
throw createSqlError(message, errorDetails);
298+
throw new SqlError(message, errorDetails);
300299
}
301300

302301
if (!parseTreePtr) {

versions/15/src/index.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,17 @@ export interface SqlErrorFormatOptions {
3030
maxQueryLength?: number; // Max query length to display (default: no limit)
3131
}
3232

33-
// Helper function to create enhanced error with SQL details
34-
function createSqlError(message: string, details: SqlErrorDetails): Error {
35-
const error = new Error(message);
36-
// Attach error details as properties
37-
Object.defineProperty(error, 'sqlDetails', {
38-
value: details,
39-
enumerable: true,
40-
configurable: true
41-
});
42-
return error;
33+
export class SqlError extends Error {
34+
sqlDetails?: SqlErrorDetails;
35+
36+
constructor(message: string, details?: SqlErrorDetails) {
37+
super(message);
38+
this.name = 'SqlError';
39+
this.sqlDetails = details;
40+
}
4341
}
4442

43+
4544
// Helper function to classify error source
4645
function getErrorSource(filename: string | null): string {
4746
if (!filename) return 'unknown';
@@ -227,7 +226,7 @@ export const parse = awaitInit(async (query: string) => {
227226
context: contextPtr ? wasmModule.UTF8ToString(contextPtr) : undefined
228227
};
229228

230-
throw createSqlError(message, errorDetails);
229+
throw new SqlError(message, errorDetails);
231230
}
232231

233232
if (!parseTreePtr) {
@@ -296,7 +295,7 @@ export function parseSync(query: string) {
296295
context: contextPtr ? wasmModule.UTF8ToString(contextPtr) : undefined
297296
};
298297

299-
throw createSqlError(message, errorDetails);
298+
throw new SqlError(message, errorDetails);
300299
}
301300

302301
if (!parseTreePtr) {

versions/16/src/index.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,17 @@ export interface SqlErrorFormatOptions {
3030
maxQueryLength?: number; // Max query length to display (default: no limit)
3131
}
3232

33-
// Helper function to create enhanced error with SQL details
34-
function createSqlError(message: string, details: SqlErrorDetails): Error {
35-
const error = new Error(message);
36-
// Attach error details as properties
37-
Object.defineProperty(error, 'sqlDetails', {
38-
value: details,
39-
enumerable: true,
40-
configurable: true
41-
});
42-
return error;
33+
export class SqlError extends Error {
34+
sqlDetails?: SqlErrorDetails;
35+
36+
constructor(message: string, details?: SqlErrorDetails) {
37+
super(message);
38+
this.name = 'SqlError';
39+
this.sqlDetails = details;
40+
}
4341
}
4442

43+
4544
// Helper function to classify error source
4645
function getErrorSource(filename: string | null): string {
4746
if (!filename) return 'unknown';
@@ -227,7 +226,7 @@ export const parse = awaitInit(async (query: string) => {
227226
context: contextPtr ? wasmModule.UTF8ToString(contextPtr) : undefined
228227
};
229228

230-
throw createSqlError(message, errorDetails);
229+
throw new SqlError(message, errorDetails);
231230
}
232231

233232
if (!parseTreePtr) {
@@ -296,7 +295,7 @@ export function parseSync(query: string) {
296295
context: contextPtr ? wasmModule.UTF8ToString(contextPtr) : undefined
297296
};
298297

299-
throw createSqlError(message, errorDetails);
298+
throw new SqlError(message, errorDetails);
300299
}
301300

302301
if (!parseTreePtr) {

versions/17/src/index.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ export interface SqlErrorFormatOptions {
3030
maxQueryLength?: number; // Max query length to display (default: no limit)
3131
}
3232

33-
// Helper function to create enhanced error with SQL details
34-
function createSqlError(message: string, details: SqlErrorDetails): Error {
35-
const error = new Error(message);
36-
// Attach error details as properties
37-
Object.defineProperty(error, 'sqlDetails', {
38-
value: details,
39-
enumerable: true,
40-
configurable: true
41-
});
42-
return error;
33+
export class SqlError extends Error {
34+
sqlDetails?: SqlErrorDetails;
35+
36+
constructor(message: string, details?: SqlErrorDetails) {
37+
super(message);
38+
this.name = 'SqlError';
39+
this.sqlDetails = details;
40+
}
4341
}
4442

43+
44+
4545
// Helper function to classify error source
4646
function getErrorSource(filename: string | null): string {
4747
if (!filename) return 'unknown';
@@ -227,7 +227,7 @@ export const parse = awaitInit(async (query: string) => {
227227
context: contextPtr ? wasmModule.UTF8ToString(contextPtr) : undefined
228228
};
229229

230-
throw createSqlError(message, errorDetails);
230+
throw new SqlError(message, errorDetails);
231231
}
232232

233233
if (!parseTreePtr) {
@@ -296,7 +296,7 @@ export function parseSync(query: string) {
296296
context: contextPtr ? wasmModule.UTF8ToString(contextPtr) : undefined
297297
};
298298

299-
throw createSqlError(message, errorDetails);
299+
throw new SqlError(message, errorDetails);
300300
}
301301

302302
if (!parseTreePtr) {

0 commit comments

Comments
 (0)