1- import { UniqueConstraintError } from '@loopback/repository' ;
1+ import {
2+ UniqueConstraintError ,
3+ QueryTimeoutError ,
4+ TableNotFoundError ,
5+ DatabaseConnectionError ,
6+ } from '@loopback/repository' ;
27import { Client , createRestAppClient , expect } from '@loopback/testlab' ;
38import { get , RestApplication , RestBindings } from '../..' ;
49
@@ -7,10 +12,25 @@ describe('RejectProvider - DATABASE_ERROR_MAPPING_OPTIONS', () => {
712 let client : Client ;
813
914 class TestController {
10- @get ( '/test-db -error' )
11- testDbError ( ) {
15+ @get ( '/test-unique -error' )
16+ testUniqueError ( ) {
1217 throw new UniqueConstraintError ( 'Duplicate key error' ) ;
1318 }
19+
20+ @get ( '/test-timeout-error' )
21+ testTimeoutError ( ) {
22+ throw new QueryTimeoutError ( 'Query timed out' ) ;
23+ }
24+
25+ @get ( '/test-table-not-found' )
26+ testTableNotFound ( ) {
27+ throw new TableNotFoundError ( 'Table does not exist' ) ;
28+ }
29+
30+ @get ( '/test-connection-error' )
31+ testConnectionError ( ) {
32+ throw new DatabaseConnectionError ( 'Connection failed' ) ;
33+ }
1434 }
1535
1636 beforeEach ( ( ) => {
@@ -24,26 +44,46 @@ describe('RejectProvider - DATABASE_ERROR_MAPPING_OPTIONS', () => {
2444 } ) ;
2545
2646 it ( 'maps database error to HTTP 409 Conflict by default' , async ( ) => {
47+ app . bind ( RestBindings . DATABASE_ERROR_MAPPING_OPTIONS ) . to ( { enabled : true } ) ;
48+ await app . start ( ) ;
49+ client = createRestAppClient ( app ) ;
50+
51+ await client . get ( '/test-unique-error' ) . expect ( 409 ) ;
52+ } ) ;
53+
54+ it ( 'maps query timeout error to HTTP 504 Gateway Timeout' , async ( ) => {
55+ app . bind ( RestBindings . DATABASE_ERROR_MAPPING_OPTIONS ) . to ( { enabled : true } ) ;
56+ await app . start ( ) ;
57+ client = createRestAppClient ( app ) ;
58+
59+ const res = await client . get ( '/test-timeout-error' ) . expect ( 504 ) ;
60+ expect ( res . body . error . statusCode ) . to . equal ( 504 ) ;
61+ } ) ;
62+
63+ it ( 'maps table not found error to HTTP 400 Bad Request' , async ( ) => {
64+ app . bind ( RestBindings . DATABASE_ERROR_MAPPING_OPTIONS ) . to ( { enabled : true } ) ;
2765 await app . start ( ) ;
2866 client = createRestAppClient ( app ) ;
2967
30- await client . get ( '/test-db-error' ) . expect ( 409 ) ;
68+ const res = await client . get ( '/test-table-not-found' ) . expect ( 400 ) ;
69+ expect ( res . body . error . statusCode ) . to . equal ( 400 ) ;
3170 } ) ;
3271
33- it ( 'maps database error to 409 Conflict when explicitly enabled ' , async ( ) => {
72+ it ( 'maps database connection error to HTTP 503 Service Unavailable ' , async ( ) => {
3473 app . bind ( RestBindings . DATABASE_ERROR_MAPPING_OPTIONS ) . to ( { enabled : true } ) ;
3574 await app . start ( ) ;
3675 client = createRestAppClient ( app ) ;
3776
38- await client . get ( '/test-db-error' ) . expect ( 409 ) ;
77+ const res = await client . get ( '/test-connection-error' ) . expect ( 503 ) ;
78+ expect ( res . body . error . statusCode ) . to . equal ( 503 ) ;
3979 } ) ;
4080
4181 it ( 'bypasses error mapping (returns 500) when disabled via app.bind()' , async ( ) => {
4282 app . bind ( RestBindings . DATABASE_ERROR_MAPPING_OPTIONS ) . to ( { enabled : false } ) ;
4383 await app . start ( ) ;
4484 client = createRestAppClient ( app ) ;
4585
46- const res = await client . get ( '/test-db -error' ) . expect ( 500 ) ;
86+ const res = await client . get ( '/test-unique -error' ) . expect ( 500 ) ;
4787 expect ( res . body . error . statusCode ) . to . equal ( 500 ) ;
4888 } ) ;
4989} ) ;
0 commit comments