Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add value property to db errors #986

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const hanaKeywords = keywords.reduce((prev, curr) => {

const DEBUG = cds.debug('sql|db')
let HANAVERSION = 0
const SANITIZE_VALUES = process.env.NODE_ENV === 'production' && cds.env.log.sanitize_values !== false

/**
* @implements SQLService
Expand Down Expand Up @@ -180,7 +181,7 @@ class HANAService extends SQLService {
: this.ensureDBC() && ps.run())
return new this.class.InsertResults(cqn, results)
} catch (err) {
throw _not_unique(err, 'ENTITY_ALREADY_EXISTS')
throw _not_unique(err, 'ENTITY_ALREADY_EXISTS', data)
}
}

Expand Down Expand Up @@ -1411,13 +1412,14 @@ SELECT ${mixing} FROM JSON_TABLE(SRC.JSON, '$' COLUMNS(${extraction})) AS NEW LE
const createContainerDatabase = fs.readFileSync(path.resolve(__dirname, 'scripts/container-database.sql'), 'utf-8')
const createContainerTenant = fs.readFileSync(path.resolve(__dirname, 'scripts/container-tenant.sql'), 'utf-8')

function _not_unique(err, code) {
function _not_unique(err, code, data) {
if (err.code === 301)
return Object.assign(err, {
originalMessage: err.message, // FIXME: required because of next line
message: code, // FIXME: misusing message as code
code: 400, // FIXME: misusing code as (http) status
})
if (data) err.values = SANITIZE_VALUES ? ['***'] : data
return err
}

Expand Down
8 changes: 5 additions & 3 deletions postgres/lib/PostgresService.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const cds = require('@sap/cds')
const crypto = require('crypto')
const { Writable, Readable } = require('stream')
const sessionVariableMap = require('./session.json')
const SANITIZE_VALUES = process.env.NODE_ENV === 'production' && cds.env.log.sanitize_values !== false

class PostgresService extends SQLService {
init() {
Expand Down Expand Up @@ -329,15 +330,15 @@ GROUP BY k
try {
return await super.onINSERT(req)
} catch (err) {
throw _not_unique(err, 'ENTITY_ALREADY_EXISTS')
throw _not_unique(err, 'ENTITY_ALREADY_EXISTS', req.data)
}
}

async onUPDATE(req) {
try {
return await super.onUPDATE(req)
} catch (err) {
throw _not_unique(err, 'UNIQUE_CONSTRAINT_VIOLATION')
throw _not_unique(err, 'UNIQUE_CONSTRAINT_VIOLATION', req.data)
}
}

Expand Down Expand Up @@ -869,13 +870,14 @@ class ParameterStream extends Writable {
}
}

function _not_unique(err, code) {
function _not_unique(err, code, data) {
if (err.code === '23505')
return Object.assign(err, {
originalMessage: err.message, // FIXME: required because of next line
message: code, // FIXME: misusing message as code
code: 400, // FIXME: misusing code as (http) status
})
if (data) err.values = SANITIZE_VALUES ? ['***'] : data
return err
}

Expand Down
8 changes: 5 additions & 3 deletions sqlite/lib/SQLiteService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const $session = Symbol('dbc.session')
const convStrm = require('stream/consumers')
const { Readable } = require('stream')

const SANITIZE_VALUES = process.env.NODE_ENV === 'production' && cds.env.log.sanitize_values !== false
const keywords = cds.compiler.to.sql.sqlite.keywords
// keywords come as array
const sqliteKeywords = keywords.reduce((prev, curr) => {
Expand Down Expand Up @@ -268,15 +269,15 @@ class SQLiteService extends SQLService {
try {
return await super.onINSERT(req)
} catch (err) {
throw _not_unique(err, 'ENTITY_ALREADY_EXISTS')
throw _not_unique(err, 'ENTITY_ALREADY_EXISTS', req.data)
}
}

async onUPDATE(req) {
try {
return await super.onUPDATE(req)
} catch (err) {
throw _not_unique(err, 'UNIQUE_CONSTRAINT_VIOLATION')
throw _not_unique(err, 'UNIQUE_CONSTRAINT_VIOLATION', req.data)
}
}
}
Expand All @@ -289,13 +290,14 @@ class SQLiteService extends SQLService {
// })
// }

function _not_unique(err, code) {
function _not_unique(err, code, data) {
if (err.message.match(/unique constraint/i))
return Object.assign(err, {
originalMessage: err.message, // FIXME: required because of next line
message: code, // FIXME: misusing message as code
code: 400, // FIXME: misusing code as (http) status
})
if (data) err.values = SANITIZE_VALUES ? ['***'] : data
return err
}

Expand Down
Loading