Skip to content

Commit

Permalink
Better error logging (#86)
Browse files Browse the repository at this point in the history
* Better error logging

* Proper index counting
  • Loading branch information
dolezel authored May 22, 2017
1 parent 796d2a3 commit af94e8f
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,30 @@ export default (connection_string) => {
: resolve(result)
))
)
);
)
.catch(err => {
const { message, position } = err;
if (message && position >= 1) {
const endLineWrapIndexOf = string.indexOf('\n', position);
const endLineWrapPos = endLineWrapIndexOf >= 0 ? endLineWrapIndexOf : string.length;
const stringStart = string.substring(0, endLineWrapPos);
const stringEnd = string.substr(endLineWrapPos);
const startLineWrapPos = stringStart.lastIndexOf('\n') + 1;
const padding = ' '.repeat(position - startLineWrapPos - 1);
console.error(`Error executing:
${stringStart}
${padding}^^^^${stringEnd}
${message}
`);
} else {
console.error(`Error executing:
${string}
${err}
`);
}
throw err;
});

const select = string =>
query(string)
Expand Down

0 comments on commit af94e8f

Please sign in to comment.