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

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 { ...@@ -569,7 +569,7 @@ export interface NonNullFindOptions extends FindOptions {
/** /**
* Throw if nothing was found. * Throw if nothing was found.
*/ */
rejectOnEmpty: boolean; rejectOnEmpty: boolean | Error;
} }
/** /**
......
...@@ -4,36 +4,55 @@ import { User } from './models/User'; ...@@ -4,36 +4,55 @@ import { User } from './models/User';
import { OptimisticLockError } from '../lib/errors'; import { OptimisticLockError } from '../lib/errors';
async function test() { async function test() {
try { try {
await User.create({ username: 'john_doe' }); await User.create({ username: 'john_doe' });
} catch (e) { } catch (e) {
if (e instanceof UniqueConstraintError) { if (e instanceof UniqueConstraintError) {
console.error((e as UniqueConstraintError).sql); throw new Error((e as UniqueConstraintError).sql);
}
} }
}
try { try {
await User.findOne({ await User.findOne({
rejectOnEmpty: true, rejectOnEmpty: true,
where: { where: {
username: 'something_that_doesnt_exist', username: 'something_that_doesnt_exist',
}, },
}); });
} catch (e) { } catch (e) {
if (!(e instanceof EmptyResultError)) { if (!(e instanceof EmptyResultError)) {
console.error('should return emptyresulterror'); throw new Error('should return emptyresulterror');
}
} }
}
try {
const user: User | null = await User.findByPk(1); class CustomError extends Error {}
if (user != null) {
user.username = 'foo'; try {
user.save(); await User.findOne({
} rejectOnEmpty: new CustomError('User does not exist'),
} catch (e) { where: {
if (!(e instanceof OptimisticLockError)) { username: 'something_that_doesnt_exist',
console.log('should return OptimisticLockError'); },
} });
} 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!