Skip to main content

DatabaseError

Extends the built-in Error. Constructed internally from the server's ErrorResponse message and thrown/rejected by query(), execute() and other statement-executing methods — you do not construct it yourself. Every field mirrors one of the fields PostgreSQL includes in its ErrorResponse/NoticeResponse protocol messages (see the PostgreSQL error field docs); all are optional because the server only sends the ones it has.

import { Connection, DatabaseError } from 'postgrejs';

const connection = new Connection('postgres://localhost');
await connection.connect();
try {
await connection.query('select * from no_such_table');
} catch (e) {
if (e instanceof DatabaseError) {
console.log(e.code, e.message, e.position);
}
}

Properties

KeyTypeReadonlyDescription
messagestringfalseHuman-readable error message (inherited from Error)
severitystringfalseERROR, FATAL, PANIC, or (for a notice) WARNING, NOTICE, DEBUG, INFO, LOG
codestringfalseSQLSTATE error code
detailstringfalseSecondary, more detailed error message
hintstringfalseSuggestion of what to do about the problem
positionnumberfalse1-based character index into the original query string where the error occurred
internalPositionstringfalse1-based character index into an internally generated query
internalQuerystringfalseThe internally generated query that caused the error
wherestringfalseContext in which the error occurred (e.g. a stack of function calls)
schemastringfalseName of the schema associated with the error
tablestringfalseName of the table associated with the error
columnstringfalseName of the column associated with the error
dataTypestringfalseName of the data type associated with the error
constraintstringfalseName of the constraint associated with the error
lineNrnumberfalseLine number within the SQL script that caused the error (computed client-side from position)
colNrnumberfalseColumn number within that line (computed client-side from position)
linestringfalseThe text of the offending line itself (computed client-side from position)
note

lineNr, colNr and line are not sent by the server — they are computed by Connection from position after the error is received, and only when position is present. They are what make the appended at line N column M context in a thrown error's message possible.