Skip to content

Commit

Permalink
add req.data to _not_unique
Browse files Browse the repository at this point in the history
  • Loading branch information
larslutz96 committed Jan 21, 2025
1 parent fe4f66b commit 1ac9900
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
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

0 comments on commit 1ac9900

Please sign in to comment.