1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Server: Display more detailed error messages on SQL query errors

This commit is contained in:
Laurent Cozic 2021-11-16 11:31:13 +00:00
parent 3716972829
commit 42a4edb19c

View File

@ -185,6 +185,27 @@ export function setupSlowQueryLog(connection: DbConnection, slowQueryLogMinDurat
});
}
const filterBindings = (bindings: any[]): Record<string, any> => {
const output: Record<string, any> = {};
for (let i = 0; i < bindings.length; i++) {
let value = bindings[i];
if (typeof value === 'string') value = value.substr(0, 200);
if (Buffer.isBuffer(value)) value = '<binary>';
output[`$${i + 1}`] = value;
}
return output;
};
interface KnexQueryErrorResponse {
message: string;
}
interface KnexQueryErrorData {
bindings: any[];
}
export async function connectDb(dbConfig: DatabaseConfig): Promise<DbConnection> {
const connection = knex(makeKnexConfig(dbConfig));
@ -192,6 +213,13 @@ export async function connectDb(dbConfig: DatabaseConfig): Promise<DbConnection>
setupSlowQueryLog(connection, dbConfig.slowQueryLogMinDuration);
}
connection.on('query-error', (response: KnexQueryErrorResponse, data: KnexQueryErrorData) => {
const msg: string[] = [];
msg.push(response.message);
if (data.bindings && data.bindings.length) msg.push(JSON.stringify(filterBindings(data.bindings), null, ' '));
logger.error(...msg);
});
return connection;
}