mirror: add default typename-mismatch error message (#1610)

Summary:
Previously, `_nontransactionallyRegisterObject` differed from its
counterpart `registerObject` in two ways: the former does not enter a
transaction, but it also requires a function to format an error message.
The value provided by `registerObject` to the helper is a suitable
default, so we can use it to decouple the transaction semantics from the
error message.

Test Plan:
Existing tests suffice, retaining full coverage.

wchargin-branch: mirror-default-typename-message
This commit is contained in:
William Chargin 2020-02-03 17:50:30 -08:00 committed by GitHub
parent 9539a18a67
commit ba9cc43a39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -365,14 +365,7 @@ export class Mirror {
+id: Schema.ObjectId, +id: Schema.ObjectId,
|}): void { |}): void {
_inTransaction(this._db, () => { _inTransaction(this._db, () => {
this._nontransactionallyRegisterObject(object, (guess) => { this._nontransactionallyRegisterObject(object);
const s = JSON.stringify;
const message =
`object ${s(object.id)} ` +
`looks like it should have type ${s(guess)}, ` +
`not ${s(object.typename)}`;
return message;
});
}); });
} }
@ -386,11 +379,22 @@ export class Mirror {
+typename: Schema.Typename, +typename: Schema.Typename,
+id: Schema.ObjectId, +id: Schema.ObjectId,
|}, |},
guessMismatchMessage: (guess: Schema.Typename) => string guessMismatchMessage?: (guess: Schema.Typename) => string
): void { ): void {
const db = this._db; const db = this._db;
const {typename, id} = object; const {typename, id} = object;
if (guessMismatchMessage == null) {
guessMismatchMessage = (guess) => {
const s = JSON.stringify;
const message =
`object ${s(object.id)} ` +
`looks like it should have type ${s(guess)}, ` +
`not ${s(object.typename)}`;
return message;
};
}
const existingTypename = db const existingTypename = db
.prepare("SELECT typename FROM objects WHERE id = ?") .prepare("SELECT typename FROM objects WHERE id = ?")
.pluck() .pluck()