Skip to content

Commit 56e245b

Browse files
committed
addressing PR comments
1 parent efbeb17 commit 56e245b

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed

packages/orm/src/client/crud/operations/base.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import {
2626
createInvalidInputError,
2727
createNotFoundError,
2828
createNotSupportedError,
29+
ORMError,
30+
ORMErrorReason,
2931
} from '../../errors';
3032
import type { ToKysely } from '../../query-builder';
3133
import {
@@ -2138,7 +2140,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
21382140
protected async executeQueryTakeFirstOrThrow(kysely: ToKysely<Schema>, query: Compilable, operation: string) {
21392141
const result = await kysely.executeQuery(query.compile(), this.makeQueryId(operation));
21402142
if (result.rows.length === 0) {
2141-
throw createNotFoundError('No rows found');
2143+
throw new ORMError(ORMErrorReason.NOT_FOUND, 'No rows found');
21422144
}
21432145
return result.rows[0];
21442146
}

packages/orm/src/client/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export function createInvalidInputError(message: string, model?: string, options
122122
export function createDBQueryError(message: string, dbError: unknown, sql: string, parameters: readonly unknown[]) {
123123
const error = new ORMError(ORMErrorReason.DB_QUERY_ERROR, message, { cause: dbError });
124124
error.dbErrorCode = getDbErrorCode(dbError);
125+
error.dbErrorMessage = dbError instanceof Error ? dbError.message : undefined;
125126
error.sql = sql;
126127
error.sqlParams = parameters;
127128
return error;

packages/server/src/api/rpc/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ export class RPCApiHandler<Schema extends SchemaDef> implements ApiHandler<Schem
212212
.with(ORMErrorReason.DB_QUERY_ERROR, () => {
213213
status = 400;
214214
error.dbErrorCode = err.dbErrorCode;
215-
});
215+
})
216+
.otherwise(() => {});
216217

217218
const resp = { status, body: { error } };
218219
log(this.options.log, 'debug', () => `sending error response: ${safeJSONStringify(resp)}`);

tests/e2e/orm/client-api/error-handling.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ model User {
4343

4444
await expect(db.user.create({ data: { email: '[email protected]' } })).rejects.toSatisfy(
4545
(e) =>
46-
e instanceof ORMError && e.reason === ORMErrorReason.DB_QUERY_ERROR && e.dbErrorCode === expectedCode,
46+
e instanceof ORMError &&
47+
e.reason === ORMErrorReason.DB_QUERY_ERROR &&
48+
e.dbErrorCode === expectedCode &&
49+
!!e.dbErrorMessage?.includes('constraint'),
4750
);
4851
});
4952
});

0 commit comments

Comments
 (0)