Skip to content

Instantly share code, notes, and snippets.

@AndrewRayCode
Created May 1, 2015 21:23
Show Gist options
  • Save AndrewRayCode/03cf7f7aad087bf33aa5 to your computer and use it in GitHub Desktop.
Save AndrewRayCode/03cf7f7aad087bf33aa5 to your computer and use it in GitHub Desktop.
Sane knex errors
}).catch(function( error ) {
var errorPosition = Number(error.position),
msg = error.toString(),
query = msg.replace(/^.+: | - .+?$/g, ''),
sqlMsg = msg.replace(/^.+ - /g, ''),
context = 50,
leftTruncate = Math.max( errorPosition - context, 0 );
console.error( query + ';' );
console.error( error.severity + ': ' + sqlMsg );
console.error(
( leftTruncate ? '…' : '' ) +
query.substring(
Math.max( errorPosition - context, 0 ),
Math.max( errorPosition + context )
) +
( errorPosition + context < query.length ? '…' : '' )
);
console.error( new Array( errorPosition - leftTruncate + !!leftTruncate ).join(' ') + '^' );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment