|
| 1 | +// Copyright IBM Corp. and LoopBack contributors 2019,2020. All Rights Reserved. |
| 2 | +// Node module: @loopback/repository |
| 3 | +// This file is licensed under the MIT License. |
| 4 | +// License text available at https://opensource.org/licenses/MIT |
| 5 | + |
| 6 | +import {expect} from '@loopback/testlab'; |
| 7 | + |
| 8 | +import { |
| 9 | + CheckConstraintError, |
| 10 | + DatabaseConnectionError, |
| 11 | + DatabaseError, |
| 12 | + DataTypeMismatchError, |
| 13 | + ForeignKeyConstraintError, |
| 14 | + LockConflictError, |
| 15 | + NotNullConstraintError, |
| 16 | + UniqueConstraintError, |
| 17 | +} from '../../..'; |
| 18 | +import {handleRepositoryError} from '../../../errors/handle-repository-error'; |
| 19 | + |
| 20 | +describe('handleRepositoryError', () => { |
| 21 | + it('throws DatabaseError for a falsy error', () => { |
| 22 | + expect(() => handleRepositoryError(null)).to.throwError( |
| 23 | + new DatabaseError('An unknown database execution error occurred.'), |
| 24 | + ); |
| 25 | + }); |
| 26 | + |
| 27 | + it('passes through an existing DatabaseError', () => { |
| 28 | + const error = new DatabaseError('Database error'); |
| 29 | + |
| 30 | + expect(() => handleRepositoryError(error)).to.throwError(error); |
| 31 | + }); |
| 32 | + |
| 33 | + it('maps UNIQUE_CONSTRAINT_VIOLATION', () => { |
| 34 | + const error = givenConnectorError( |
| 35 | + 'UNIQUE_CONSTRAINT_VIOLATION', |
| 36 | + 'Duplicate value', |
| 37 | + ); |
| 38 | + |
| 39 | + expect(() => handleRepositoryError(error)).to.throwError( |
| 40 | + UniqueConstraintError, |
| 41 | + ); |
| 42 | + |
| 43 | + expect(() => handleRepositoryError(error)).to.throwError(/Duplicate value/); |
| 44 | + }); |
| 45 | + |
| 46 | + it('maps FOREIGN_KEY_VIOLATION', () => { |
| 47 | + const error = givenConnectorError( |
| 48 | + 'FOREIGN_KEY_VIOLATION', |
| 49 | + 'Foreign key violation', |
| 50 | + ); |
| 51 | + |
| 52 | + expect(() => handleRepositoryError(error)).to.throwError( |
| 53 | + ForeignKeyConstraintError, |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it('maps NOT_NULL_VIOLATION', () => { |
| 58 | + const error = givenConnectorError( |
| 59 | + 'NOT_NULL_VIOLATION', |
| 60 | + 'Not-null violation', |
| 61 | + ); |
| 62 | + |
| 63 | + expect(() => handleRepositoryError(error)).to.throwError( |
| 64 | + NotNullConstraintError, |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('maps CHECK_CONSTRAINT_VIOLATION', () => { |
| 69 | + const error = givenConnectorError( |
| 70 | + 'CHECK_CONSTRAINT_VIOLATION', |
| 71 | + 'Check constraint violation', |
| 72 | + ); |
| 73 | + |
| 74 | + expect(() => handleRepositoryError(error)).to.throwError( |
| 75 | + CheckConstraintError, |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + it('maps DATA_TYPE_MISMATCH', () => { |
| 80 | + const error = givenConnectorError( |
| 81 | + 'DATA_TYPE_MISMATCH', |
| 82 | + 'Data type mismatch', |
| 83 | + ); |
| 84 | + |
| 85 | + expect(() => handleRepositoryError(error)).to.throwError( |
| 86 | + DataTypeMismatchError, |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it('maps LOCK_CONFLICT', () => { |
| 91 | + const error = givenConnectorError('LOCK_CONFLICT', 'Lock conflict'); |
| 92 | + |
| 93 | + expect(() => handleRepositoryError(error)).to.throwError(LockConflictError); |
| 94 | + }); |
| 95 | + |
| 96 | + it('maps CONNECTION_FAILURE', () => { |
| 97 | + const error = givenConnectorError( |
| 98 | + 'CONNECTION_FAILURE', |
| 99 | + 'Connection failure', |
| 100 | + ); |
| 101 | + |
| 102 | + expect(() => handleRepositoryError(error)).to.throwError( |
| 103 | + DatabaseConnectionError, |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it('preserves message and details when mapping an error', () => { |
| 108 | + const details = { |
| 109 | + constraint: 'users_email_key', |
| 110 | + field: 'email', |
| 111 | + }; |
| 112 | + |
| 113 | + const error = { |
| 114 | + code: 'UNIQUE_CONSTRAINT_VIOLATION', |
| 115 | + message: 'Email already exists', |
| 116 | + details, |
| 117 | + }; |
| 118 | + |
| 119 | + try { |
| 120 | + handleRepositoryError(error); |
| 121 | + } catch (err) { |
| 122 | + expect(err).to.be.instanceof(UniqueConstraintError); |
| 123 | + expect((err as UniqueConstraintError).message).to.equal( |
| 124 | + 'Email already exists', |
| 125 | + ); |
| 126 | + expect((err as UniqueConstraintError).details).to.eql(details); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + it('rethrows an unmapped connector error as-is', () => { |
| 131 | + const error = { |
| 132 | + code: 'SOME_UNKNOWN_ERROR', |
| 133 | + message: 'Unknown connector error', |
| 134 | + }; |
| 135 | + |
| 136 | + try { |
| 137 | + handleRepositoryError(error); |
| 138 | + } catch (err) { |
| 139 | + expect(err).to.equal(error); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + it('rethrows a generic Error as-is', () => { |
| 144 | + const error = new Error('Something went wrong'); |
| 145 | + |
| 146 | + try { |
| 147 | + handleRepositoryError(error); |
| 148 | + } catch (err) { |
| 149 | + expect(err).to.equal(error); |
| 150 | + } |
| 151 | + }); |
| 152 | + |
| 153 | + it('rethrows an object without a code as-is', () => { |
| 154 | + const error = { |
| 155 | + message: 'Something went wrong', |
| 156 | + }; |
| 157 | + |
| 158 | + try { |
| 159 | + handleRepositoryError(error); |
| 160 | + } catch (err) { |
| 161 | + expect(err).to.equal(error); |
| 162 | + } |
| 163 | + }); |
| 164 | +}); |
| 165 | + |
| 166 | +function givenConnectorError(code: string, message: string) { |
| 167 | + return { |
| 168 | + code, |
| 169 | + message, |
| 170 | + details: { |
| 171 | + source: 'test', |
| 172 | + }, |
| 173 | + }; |
| 174 | +} |
0 commit comments