Merge pull request #13657 from Budibase/fix/databaseimpl-error-leak

Improving DatabaseImpl error handling
This commit is contained in:
Michael Drury 2024-05-10 12:08:51 +01:00 committed by GitHub
commit 0e35ecd841
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 10 deletions

View File

@ -3,11 +3,11 @@ import {
AllDocsResponse, AllDocsResponse,
AnyDocument, AnyDocument,
Database, Database,
DatabaseOpts,
DatabaseQueryOpts,
DatabasePutOpts,
DatabaseCreateIndexOpts, DatabaseCreateIndexOpts,
DatabaseDeleteIndexOpts, DatabaseDeleteIndexOpts,
DatabaseOpts,
DatabasePutOpts,
DatabaseQueryOpts,
Document, Document,
isDocument, isDocument,
RowResponse, RowResponse,
@ -17,7 +17,7 @@ import {
import { getCouchInfo } from "./connections" import { getCouchInfo } from "./connections"
import { directCouchUrlCall } from "./utils" import { directCouchUrlCall } from "./utils"
import { getPouchDB } from "./pouchDB" import { getPouchDB } from "./pouchDB"
import { WriteStream, ReadStream } from "fs" import { ReadStream, WriteStream } from "fs"
import { newid } from "../../docIds/newid" import { newid } from "../../docIds/newid"
import { SQLITE_DESIGN_DOC_ID } from "../../constants" import { SQLITE_DESIGN_DOC_ID } from "../../constants"
import { DDInstrumentedDatabase } from "../instrumentation" import { DDInstrumentedDatabase } from "../instrumentation"
@ -38,6 +38,39 @@ function buildNano(couchInfo: { url: string; cookie: string }) {
type DBCall<T> = () => Promise<T> type DBCall<T> = () => Promise<T>
class CouchDBError extends Error {
status: number
statusCode: number
reason: string
name: string
errid: string
error: string
description: string
constructor(
message: string,
info: {
status: number | undefined
statusCode: number | undefined
name: string
errid: string
description: string
reason: string
error: string
}
) {
super(message)
const statusCode = info.status || info.statusCode || 500
this.status = statusCode
this.statusCode = statusCode
this.reason = info.reason
this.name = info.name
this.errid = info.errid
this.description = info.description
this.error = info.error
}
}
export function DatabaseWithConnection( export function DatabaseWithConnection(
dbName: string, dbName: string,
connection: string, connection: string,
@ -119,7 +152,7 @@ export class DatabaseImpl implements Database {
} catch (err: any) { } catch (err: any) {
// Handling race conditions // Handling race conditions
if (err.statusCode !== 412) { if (err.statusCode !== 412) {
throw err throw new CouchDBError(err.message, err)
} }
} }
} }
@ -138,10 +171,9 @@ export class DatabaseImpl implements Database {
if (err.statusCode === 404 && err.reason === DATABASE_NOT_FOUND) { if (err.statusCode === 404 && err.reason === DATABASE_NOT_FOUND) {
await this.checkAndCreateDb() await this.checkAndCreateDb()
return await this.performCall(call) return await this.performCall(call)
} else if (err.statusCode) {
err.status = err.statusCode
} }
throw err // stripping the error down the props which are safe/useful, drop everything else
throw new CouchDBError(`CouchDB error: ${err.message}`, err)
} }
} }
@ -288,7 +320,7 @@ export class DatabaseImpl implements Database {
if (err.statusCode === 404) { if (err.statusCode === 404) {
return return
} else { } else {
throw { ...err, status: err.statusCode } throw new CouchDBError(err.message, err)
} }
} }
} }

@ -1 +1 @@
Subproject commit 479879246aac5dd3073cc695945c62c41fae5b0e Subproject commit ff397e5454ad3361b25efdf14746c36dcbd3f409