Skip to content

classifyPgError

classifyPgError(error: PgErrorLike | null | undefined): {
code: string | undefined;
httpStatus: number;
message: string;
}

The structured counterpart to friendlyError. Returns the SQLSTATE code, a suggested httpStatus, and the same user-facing message — so a service can wrap it in its own typed error class without restating the copy.

import { classifyPgError } from '@smplcty/db';
try {
await client.query(sql, params);
} catch (err) {
const { httpStatus, message } = classifyPgError(err);
reply.code(httpStatus).send({ message });
}
httpStatusWhen
409unique violation
400foreign-key / restrict / not-null / truncation
503connection failure / admin shutdown
500anything else, including a missing code

The message is byte-for-byte what friendlyError returns for the same error, so the two helpers never disagree.