errors.ts
1.4 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
// Error === BaseError
import { BaseError, EmptyResultError, Error, UniqueConstraintError } from 'sequelize';
import { User } from './models/User';
import { OptimisticLockError } from '../lib/errors';
async function test() {
try {
await User.create({ username: 'john_doe' });
} catch (e) {
if (e instanceof UniqueConstraintError) {
throw new Error((e as UniqueConstraintError).sql);
}
}
try {
await User.findOne({
rejectOnEmpty: true,
where: {
username: 'something_that_doesnt_exist',
},
});
} catch (e) {
if (!(e instanceof EmptyResultError)) {
throw new Error('should return emptyresulterror');
}
}
class CustomError extends Error {}
try {
await User.findOne({
rejectOnEmpty: new CustomError('User does not exist'),
where: {
username: 'something_that_doesnt_exist',
},
});
} catch (e) {
if (!(e instanceof CustomError)) {
throw new Error('should return CustomError');
}
if (e.message !== 'User does not exist') {
throw new Error('should return CustomError with the proper message')
}
}
try {
const user: User | null = await User.findByPk(1);
if (user != null) {
user.username = 'foo';
user.save();
}
} catch (e) {
if (!(e instanceof OptimisticLockError)) {
throw new Error('should return OptimisticLockError');
}
}
}