@sap/cds-mtxs
Version:
SAP Cloud Application Programming Model - Multitenancy library
46 lines (35 loc) • 1.3 kB
JavaScript
const cds = require('@sap/cds/lib')
class ErrorCodes {
static TABLE_NOT_FOUND = 'TABLE_NOT_FOUND'
static COLUMN_NOT_FOUND = 'COLUMN_NOT_FOUND'
get TABLE_NOT_FOUND() { return this.constructor.TABLE_NOT_FOUND }
get COLUMN_NOT_FOUND() { return this.constructor.COLUMN_NOT_FOUND }
}
class SqliteErrorCodes extends ErrorCodes {
SQLITE_ERRORS = [
{ message: 'no such table', code: this.TABLE_NOT_FOUND },
{ message: 'no such column', code: this.COLUMN_NOT_FOUND }
]
static getInstance() {
if (((cds.requires.db?.kind in { 'sqlite':1, 'better-sqlite':2 }) && 'SQLite database')) return new SqliteErrorCodes()
}
getErrorCode(err) {
return this.SQLITE_ERRORS.find(({ message }) => err.message.includes(message))?.code
}
}
class HanaErrorCodes extends ErrorCodes {
// see https://help.sap.com/docs/SAP_HANA_PLATFORM/bed8c14f9f024763b0777aa72b5436f6/f789706cd2ca49b6b65655da5a8f739c.html
HANA_ERRORS = {
259: this.TABLE_NOT_FOUND,
260: this.COLUMN_NOT_FOUND
}
static getInstance() {
if (cds.requires.db?.kind === 'hana') return new HanaErrorCodes()
}
getErrorCode(err) {
if (this.HANA_ERRORS[err.code]) {
return this.HANA_ERRORS[err.code]
}
}
}
module.exports = SqliteErrorCodes.getInstance() ?? HanaErrorCodes.getInstance()