MySQL examples

Sending a detailed error

If a filter wants to send an error back to the client in a request filter, it can normally use the result object, e.g.:

context.result.success = false;
context.result.errorMessage = "Your query is rejected because reasons";
context.result.errorCode = 12345;


However, this mechanism does not allow you to set the SQL state of the error. If you need to do that, you need to create an Error packet and set it up:

let err = context.myutils.createPacket("ERR");

err.errorCode = 12345;

err.errorMessage = "Your request is denied";

err.sqlState = "FOO";

context.result.response = err;

There is also a quicker way to do the same thing using the context.utils object:

context.result.response = context.myutils.createErrorPacket(12345, "FOO", "Your request is denied");

Executing code depending on the current database user

A filter has access to the current database user's name and can therefore execute accordingly. For instance, in a result set filter, you might have something like:

if (context.connectionContext.username === 'jdoe') {

log.debug("Jane Doe is not allowed to see this data");

context.packet.ssn = "XXX-XX-XXXX";

}