不要怂,就是干,撸起袖子干!

Commit 398ae593 by Bill Li Committed by Sushant

fix(types): support error with rejectOnEmpty (#11359)

1 parent 47ee5345
Showing with 47 additions and 28 deletions
......@@ -569,7 +569,7 @@ export interface NonNullFindOptions extends FindOptions {
/**
* Throw if nothing was found.
*/
rejectOnEmpty: boolean;
rejectOnEmpty: boolean | Error;
}
/**
......
......@@ -4,36 +4,55 @@ 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) {
console.error((e as UniqueConstraintError).sql);
}
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)) {
console.error('should return emptyresulterror');
}
try {
await User.findOne({
rejectOnEmpty: true,
where: {
username: 'something_that_doesnt_exist',
},
});
} catch (e) {
if (!(e instanceof EmptyResultError)) {
throw new Error('should return emptyresulterror');
}
}
try {
const user: User | null = await User.findByPk(1);
if (user != null) {
user.username = 'foo';
user.save();
}
} catch (e) {
if (!(e instanceof OptimisticLockError)) {
console.log('should return OptimisticLockError');
}
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');
}
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!