| 1 | import { describe, it } from 'node:test' |
| 2 | |
| 3 | import { expect } from 'expect' |
| 4 | |
| 5 | import type { ChargingStation } from '../../src/charging-station/index.js' |
| 6 | import { |
| 7 | FileType, |
| 8 | GenericStatus, |
| 9 | IncomingRequestCommand, |
| 10 | MessageType, |
| 11 | RequestCommand |
| 12 | } from '../../src/types/index.js' |
| 13 | import { |
| 14 | handleFileException, |
| 15 | handleIncomingRequestError, |
| 16 | handleSendMessageError, |
| 17 | setDefaultErrorParams |
| 18 | } from '../../src/utils/ErrorUtils.js' |
| 19 | |
| 20 | await describe('ErrorUtils test suite', async () => { |
| 21 | // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 22 | const chargingStation = { |
| 23 | logPrefix: () => 'CS-TEST |' |
| 24 | } as ChargingStation |
| 25 | |
| 26 | await it('Verify handleFileException()', () => { |
| 27 | const error = new Error() |
| 28 | error.code = 'ENOENT' |
| 29 | expect(() => { |
| 30 | handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {}) |
| 31 | }).toThrow(error) |
| 32 | expect(() => { |
| 33 | handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', { |
| 34 | throwError: false |
| 35 | }) |
| 36 | }).not.toThrow() |
| 37 | }) |
| 38 | |
| 39 | await it('Verify handleSendMessageError()', () => { |
| 40 | const error = new Error() |
| 41 | expect(() => { |
| 42 | handleSendMessageError( |
| 43 | chargingStation, |
| 44 | RequestCommand.BOOT_NOTIFICATION, |
| 45 | MessageType.CALL_MESSAGE, |
| 46 | error |
| 47 | ) |
| 48 | }).not.toThrow() |
| 49 | expect(() => { |
| 50 | handleSendMessageError( |
| 51 | chargingStation, |
| 52 | RequestCommand.BOOT_NOTIFICATION, |
| 53 | MessageType.CALL_MESSAGE, |
| 54 | error, |
| 55 | { throwError: true } |
| 56 | ) |
| 57 | }).toThrow(error) |
| 58 | }) |
| 59 | |
| 60 | await it('Verify handleIncomingRequestError()', () => { |
| 61 | const error = new Error() |
| 62 | expect(() => { |
| 63 | handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error) |
| 64 | }).toThrow(error) |
| 65 | expect(() => { |
| 66 | handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error, { |
| 67 | throwError: false |
| 68 | }) |
| 69 | }).not.toThrow() |
| 70 | const errorResponse = { |
| 71 | status: GenericStatus.Rejected |
| 72 | } |
| 73 | expect( |
| 74 | handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error, { |
| 75 | throwError: false, |
| 76 | errorResponse |
| 77 | }) |
| 78 | ).toStrictEqual(errorResponse) |
| 79 | }) |
| 80 | |
| 81 | await it('Verify setDefaultErrorParams()', () => { |
| 82 | expect(setDefaultErrorParams({})).toStrictEqual({ throwError: true, consoleOut: false }) |
| 83 | expect(setDefaultErrorParams({ throwError: false })).toStrictEqual({ |
| 84 | throwError: false, |
| 85 | consoleOut: false |
| 86 | }) |
| 87 | expect(setDefaultErrorParams({ throwError: false, consoleOut: true })).toStrictEqual({ |
| 88 | throwError: false, |
| 89 | consoleOut: true |
| 90 | }) |
| 91 | expect(setDefaultErrorParams({ throwError: true, consoleOut: true })).toStrictEqual({ |
| 92 | throwError: true, |
| 93 | consoleOut: true |
| 94 | }) |
| 95 | expect(setDefaultErrorParams({}, { throwError: false, consoleOut: false })).toStrictEqual({ |
| 96 | throwError: false, |
| 97 | consoleOut: false |
| 98 | }) |
| 99 | }) |
| 100 | }) |