-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathimport-runner-error-sanitize.test.ts
More file actions
66 lines (56 loc) · 2.67 KB
/
Copy pathimport-runner-error-sanitize.test.ts
File metadata and controls
66 lines (56 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
/**
* sanitizeRowError() — driver/query-builder errors embed the whole failing SQL
* statement in `err.message`; it must never reach the importer verbatim
* (framework#3566). Common constraint failures map to human wording.
*/
import { describe, it, expect } from 'vitest';
import { sanitizeRowError } from './import-runner';
describe('sanitizeRowError', () => {
it('maps a sqlite UNIQUE violation to a friendly, SQL-free message', () => {
const raw =
"insert into `sys_user` (`email`, `name`, `phone_number`) values " +
"('a@b.com', 'zhoujunyi', '13800000000') - UNIQUE constraint failed: sys_user.phone_number";
const out = sanitizeRowError(raw);
expect(out).toBe('A record with this phone_number already exists.');
expect(out).not.toMatch(/insert into/i);
});
it('maps a mysql duplicate entry', () => {
const raw =
"insert into `sys_user` ... - ER_DUP_ENTRY: Duplicate entry 'a@b.com' for key 'sys_user.email'";
expect(sanitizeRowError(raw)).toBe('A record with this email already exists.');
});
it('maps a postgres unique violation', () => {
const raw =
'insert into "sys_user" ... - duplicate key value violates unique constraint "sys_user_email_key" ' +
'Detail: Key (email)=(a@b.com) already exists.';
expect(sanitizeRowError(raw)).toBe('A record with this email already exists.');
});
it('maps a NOT NULL violation', () => {
const raw = 'insert into `sys_user` (...) values (...) - NOT NULL constraint failed: sys_user.name';
expect(sanitizeRowError(raw)).toBe('name is required.');
});
it('never leaks a raw SQL statement even for an unrecognized reason', () => {
const raw = 'insert into `sys_user` (`email`) values (?) - some cryptic driver failure';
const out = sanitizeRowError(raw);
expect(out).not.toMatch(/insert into/i);
// salvages the trailing non-SQL reason
expect(out).toBe('some cryptic driver failure');
});
it('falls back to a generic message when only SQL is present', () => {
const raw = 'insert into `sys_user` (`email`) values (?)';
expect(sanitizeRowError(raw)).toBe(
'The database rejected this row (a value may be invalid or already in use).',
);
});
it('passes already-friendly messages through unchanged', () => {
expect(sanitizeRowError('User already exists. Use another email.')).toBe(
'User already exists. Use another email.',
);
});
it('handles empty / non-string input', () => {
expect(sanitizeRowError('')).toBe('Row failed');
expect(sanitizeRowError(undefined)).toBe('Row failed');
expect(sanitizeRowError(null)).toBe('Row failed');
});
});