bkonkle / bs-knex Goto Github PK
View Code? Open in Web Editor NEW[DEPRECATED] BuckleScript interface to the node 'knex' library
License: BSD 2-Clause "Simplified" License
[DEPRECATED] BuckleScript interface to the node 'knex' library
License: BSD 2-Clause "Simplified" License
"bs-knex": "3.1.0"
"bs-platform": "4.0.7"
Node.js v10.15.0
I have the following javascript code in an entry point for a node app
process.on('unhandledRejection', (error, p) => {
console.error('Unhandled Rejection at: Promise', p, 'error:', error);
});
When I call into Knex with something that looks like this with something that triggers the unique error
let insert = (owner: string, repo: string) =>
Homn_Knex.knex
|> Knex.fromTable("github_release")
|> Knex.insert({"owner": owner, "repo": repo})
|> Knex.toPromise
|> KnexUtils.handleUniqueError(
~name="github_release_owner_repo_unique",
~message={j|The repository "$repo" already exists for "$owner"|j},
)
|> KnexUtils.handleDbErrors;
I get this console output (despite the rejection being handled later in the flow by reason-apollo)
Unhandled Rejection at: Promise Promise {
<rejected> { error: duplicate key value violates unique constraint "github_release_owner_repo_unique"
at Connection.parseE (/Users/wtf/src/homn-api-reason/node_modules/pg/lib/connection.js:554:11)
at Connection.parseMessage (/Users/wtf/src/homn-api-reason/node_modules/pg/lib/connection.js:379:19)
at Socket.<anonymous> (/Users/wtf/src/homn-api-reason/node_modules/pg/lib/connection.js:119:22)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
This is being caused by the promise being rejected twice here?
https://github.com/bkonkle/bs-knex/blob/master/src/KnexUtils.re#L64
and
https://github.com/bkonkle/bs-knex/blob/master/src/KnexUtils.re#L73
I ended up writing my own handleUniqueError()
and catchUniqueError()
implementations in a module outside bs-knex which don't trigger the unhandled promise rejection.
Removed the let continue = reject(Debug.toExn(exn));
line and explicitly call Js.Promise.reject where it was used
I'm still learning Reason, is that the correct way to fix this, or am I potentially misunderstanding the error? This is what I came up with.
let catchUniqueError = (~name: string, ~handle, promise) =>
Js.Nullable.(
promise
|> Js.Promise.catch(exn => {
let codeOpt = exn |> KnexUtils.exnCode |> toOption;
switch (codeOpt) {
| Some(code) =>
if (code === KnexUtils.uniqueViolation) {
let constraintOpt = exn |> KnexUtils.exnConstraint |> toOption;
switch (constraintOpt) {
| Some(constraintName) =>
if (constraintName === name) {
handle(exn);
} else {
Js.Promise.reject(Debug.toExn(exn));
}
| None => Js.Promise.reject(Debug.toExn(exn))
};
} else {
Js.Promise.reject(Debug.toExn(exn));
}
| None => Js.Promise.reject(Debug.toExn(exn))
};
})
);
let handleUniqueError = (~name: string, ~message: string) =>
catchUniqueError(~name, ~handle=_exn =>
Js.Promise.reject(KnexUtils.makeError(message))
);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.