Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add query to error option #469

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion client/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ export class PostgresError extends Error {
*/
public fields: Notice;

/**
* The query that caused the error
*/
public query: string | undefined;

/**
* Create a new PostgresError
*/
constructor(fields: Notice) {
constructor(fields: Notice, query?: string) {
super(fields.message);
this.fields = fields;
this.query = query;
this.name = "PostgresError";
}
}
Expand Down
20 changes: 18 additions & 2 deletions connection/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,15 @@ export class Connection {
while (current_message.type !== INCOMING_QUERY_MESSAGES.READY) {
switch (current_message.type) {
case ERROR_MESSAGE:
error = new PostgresError(parseNoticeMessage(current_message));
error = new PostgresError(
parseNoticeMessage(current_message),
isDebugOptionEnabled(
"queryInError",
this.#connection_params.controls?.debug,
)
? query.text
: undefined,
);
break;
case INCOMING_QUERY_MESSAGES.COMMAND_COMPLETE: {
result.handleCommandComplete(
Expand Down Expand Up @@ -881,7 +889,15 @@ export class Connection {
while (current_message.type !== INCOMING_QUERY_MESSAGES.READY) {
switch (current_message.type) {
case ERROR_MESSAGE: {
error = new PostgresError(parseNoticeMessage(current_message));
error = new PostgresError(
parseNoticeMessage(current_message),
isDebugOptionEnabled(
"queryInError",
this.#connection_params.controls?.debug,
)
? query.text
: undefined,
);
break;
}
case INCOMING_QUERY_MESSAGES.BIND_COMPLETE:
Expand Down
8 changes: 5 additions & 3 deletions debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
export type DebugControls = DebugOptions | boolean;

type DebugOptions = {
/** Log queries */
/** Log all queries */
queries?: boolean;
/** Log INFO, NOTICE, and WARNING raised database messages */
/** Log all INFO, NOTICE, and WARNING raised database messages */
notices?: boolean;
/** Log results */
/** Log all results */
results?: boolean;
/** Include the SQL query that caused an error in the PostgresError object */
queryInError?: boolean;
};

export const isDebugOptionEnabled = (
Expand Down
9 changes: 5 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1403,8 +1403,10 @@ enabled by using the `debug` option in the Client `controls` parameter. Pass
options:

- `queries` : Logs all SQL queries executed by the client
- `notices` : Logs database messages (INFO, NOTICE, WARNING))
- `results` : Logs the result of the queries
- `notices` : Logs all database messages (INFO, NOTICE, WARNING))
- `results` : Logs all the result of the queries
- `queryInError` : Includes the SQL query that caused an error in the
PostgresError object

### Example

Expand All @@ -1419,7 +1421,6 @@ const client = new Client({
port: 5432,
password: "postgres",
controls: {
// the same as `debug: true`
debug: {
queries: true,
notices: true,
Expand All @@ -1430,7 +1431,7 @@ const client = new Client({

await client.connect();

const result = await client.queryObject`SELECT public.get_some_user()`;
await client.queryObject`SELECT public.get_uuid()`;

await client.end();
```
Expand Down
38 changes: 38 additions & 0 deletions tests/query_client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import {
assert,
assertEquals,
assertInstanceOf,
assertObjectMatch,
assertRejects,
assertThrows,
Expand Down Expand Up @@ -284,6 +285,43 @@ Deno.test(
),
);

Deno.test(
"Debug query not in error",
withClient(async (client) => {
const invalid_query = "SELECT this_has $ 'syntax_error';";
try {
await client.queryObject(invalid_query);
} catch (error) {
assertInstanceOf(error, PostgresError);
assertEquals(error.message, 'syntax error at or near "$"');
assertEquals(error.query, undefined);
}
}),
);

Deno.test(
"Debug query in error",
withClient(
async (client) => {
const invalid_query = "SELECT this_has $ 'syntax_error';";
try {
await client.queryObject(invalid_query);
} catch (error) {
assertInstanceOf(error, PostgresError);
assertEquals(error.message, 'syntax error at or near "$"');
assertEquals(error.query, invalid_query);
}
},
{
controls: {
debug: {
queryInError: true,
},
},
},
),
);

Deno.test(
"Array arguments",
withClient(async (client) => {
Expand Down
1 change: 1 addition & 0 deletions tests/test_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from "../deps.ts";
export {
assert,
assertEquals,
assertInstanceOf,
assertNotEquals,
assertObjectMatch,
assertRejects,
Expand Down