Skip to content

Commit

Permalink
✨ Improve connection error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas2D committed Oct 11, 2024
1 parent d6043fe commit fab2784
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Transaction from "./transaction";
import SchemaCompiler from "./schema/compiler";
import Firebird_Formatter from "./formatter";
import Firebird_DDL from "./schema/ddl";

import { isFirebirdConnectionError } from './utils'
import * as driver from 'node-firebird-driver-native'

class Client_Firebird extends Client {
Expand Down Expand Up @@ -176,7 +176,7 @@ class Client_Firebird extends Client {
await transaction.rollback().catch(noop);
transaction = null
}
if (String(e).includes('Error writing data to the connection.')) {
if (isFirebirdConnectionError(e)) {
await this.destroyRawConnection(connection)
}
throw e
Expand Down
35 changes: 31 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
export function toArrayFromPrimitive(val) {
if (Array.isArray(val)) {
return val;
// Source: https://github.com/hgourvest/node-firebird/blob/master/lib/firebird.msg.json
export const FirebirdConnectionErrors = {
"335544324": "Invalid database handle (no active connection)",
"335544365": "Request referenced an unavailable database",
"335544375": "Unavailable database",
"335544421": "Connection rejected by remote interface",
"335544648": "Connection lost to pipe server",
"335544721": "Unable to complete network request to host",
"335544722": "Failed to establish a connection",
"335544723": "Error while listening for an incoming connection",
"335544724": "Failed to establish a secondary connection for event processing",
"335544725": "Error while listening for an incoming event connection request",
"335544726": "Error reading data from the connection",
"335544727": "Error writing data to the connection",
"335544741": "Connection lost to database",
"335544856": "Connection shutdown"
}

export function isFirebirdConnectionError(error) {
if (!error || !(error instanceof Error)) {
return false
}
if (String(error.code) in FirebirdConnectionErrors) {
return true
}

return [val];
const msg = String(error)
for (const err in FirebirdConnectionErrors) {
if (msg.includes(err)) {
return true
}
}
return false
}

0 comments on commit fab2784

Please sign in to comment.