@@ -164,6 +164,65 @@ describe('Knex Error handler', () => {
164164 )
165165 } )
166166
167+ it ( 'MySQL errors keep the driver message when values contain " - "' , ( ) => {
168+ assert . throws (
169+ ( ) =>
170+ errorHandler ( {
171+ code : 'ER_DUP_ENTRY' ,
172+ errno : 1062 ,
173+ sqlState : '23000' ,
174+ sqlMessage : "Duplicate entry 'foo - bar' for key 'name'" ,
175+ message :
176+ "insert into `t` (`name`) values ('foo - bar') - ER_DUP_ENTRY: Duplicate entry 'foo - bar' for key 'name'"
177+ } ) ,
178+ { name : 'BadRequest' , message : "ER_DUP_ENTRY: Duplicate entry 'foo - bar' for key 'name'" }
179+ )
180+ assert . throws (
181+ ( ) =>
182+ errorHandler ( {
183+ code : 'ER_DUP_ENTRY' ,
184+ errno : 1062 ,
185+ sqlState : '23000' ,
186+ sqlMessage : "Duplicate entry 'foo - bar' for key 'name'" ,
187+ message :
188+ "insert into `t` (`name`) values ('foo - bar') - Duplicate entry 'foo - bar' for key 'name'"
189+ } ) ,
190+ { name : 'BadRequest' , message : "Duplicate entry 'foo - bar' for key 'name'" }
191+ )
192+ } )
193+
194+ it ( 'SQLite errors keep the driver message when values contain " - "' , ( ) => {
195+ assert . throws (
196+ ( ) =>
197+ errorHandler ( {
198+ code : 'SQLITE_CONSTRAINT' ,
199+ errno : 19 ,
200+ message :
201+ "insert into `t` (`name`) values ('a - b') - SQLITE_CONSTRAINT: UNIQUE constraint failed: t.name - x"
202+ } ) ,
203+ { name : 'GeneralError' , message : 'SQLITE_CONSTRAINT: UNIQUE constraint failed: t.name - x' }
204+ )
205+ } )
206+
207+ it ( 'knex compile errors omit the query' , ( ) => {
208+ assert . throws (
209+ ( ) =>
210+ errorHandler (
211+ new Error (
212+ 'Undefined binding(s) detected when compiling SELECT. Undefined column(s): [users.id] query: select "users".* from "users" where "users"."id" = ?'
213+ )
214+ ) ,
215+ {
216+ name : 'GeneralError' ,
217+ message : 'Undefined binding(s) detected when compiling SELECT. Undefined column(s): [users.id]'
218+ }
219+ )
220+ } )
221+
222+ it ( 'errors without a message keep the default message' , ( ) => {
223+ assert . throws ( ( ) => errorHandler ( { code : 'ECONNRESET' } ) , { name : 'GeneralError' , message : 'Error' } )
224+ } )
225+
167226 it ( 'does not leak queries or bound values from a real database' , async ( ) => {
168227 const db = knex ( { client : 'sqlite3' , connection : { filename : ':memory:' } , useNullAsDefault : true } )
169228
@@ -187,6 +246,16 @@ describe('Knex Error handler', () => {
187246 return true
188247 }
189248 )
249+ const other = await service . create ( { email : 'b@example.com' , password : 'hash-3' } )
250+
251+ await assert . rejects (
252+ ( ) => service . patch ( other . id , { email : 'a@example.com' , password : 'secret-hash-4' } ) ,
253+ ( error : any ) => {
254+ assert . ok ( ! error . message . includes ( 'update' ) )
255+ assert . ok ( ! error . message . includes ( 'secret-hash-4' ) )
256+ return true
257+ }
258+ )
190259 await assert . rejects (
191260 ( ) => service . find ( { query : { nope : 'x' } } as any ) ,
192261 ( error : any ) => {
0 commit comments