From 653a1a77cbba78931ef03725997d9501d2529cca Mon Sep 17 00:00:00 2001 From: "anatoly.shipitz" Date: Sun, 3 Aug 2025 11:51:47 +0200 Subject: [PATCH] Add OAuth2Error class and corresponding tests - Introduced a new error class `OAuth2Error` that extends `AppError`, providing a specific error handling mechanism for OAuth2-related issues. - Added unit tests for `OAuth2Error` to verify the correct setting of message, name, and error code. These changes enhance error management in the application by allowing for more granular handling of OAuth2 errors. --- .../src/common/errors/OAuth2Error.test.ts | 21 +++++++++++++++++++ workers/main/src/common/errors/OAuth2Error.ts | 10 +++++++++ workers/main/src/common/errors/index.ts | 1 + 3 files changed, 32 insertions(+) create mode 100644 workers/main/src/common/errors/OAuth2Error.test.ts create mode 100644 workers/main/src/common/errors/OAuth2Error.ts diff --git a/workers/main/src/common/errors/OAuth2Error.test.ts b/workers/main/src/common/errors/OAuth2Error.test.ts new file mode 100644 index 0000000..cb13210 --- /dev/null +++ b/workers/main/src/common/errors/OAuth2Error.test.ts @@ -0,0 +1,21 @@ +import { describe, expect, it } from 'vitest'; + +import { OAuth2Error } from './OAuth2Error'; + +describe('OAuth2Error', () => { + it('should set the message, name, and default error code', () => { + const err = new OAuth2Error('test message'); + + expect(err.message).toBe('test message'); + expect(err.name).toBe('OAuth2Error'); + expect(err.code).toBe('UNKNOWN_OAUTH2_ERROR'); + }); + + it('should set the message, name, and custom error code', () => { + const err = new OAuth2Error('test message', 'CUSTOM_ERROR'); + + expect(err.message).toBe('test message'); + expect(err.name).toBe('OAuth2Error'); + expect(err.code).toBe('CUSTOM_ERROR'); + }); +}); diff --git a/workers/main/src/common/errors/OAuth2Error.ts b/workers/main/src/common/errors/OAuth2Error.ts new file mode 100644 index 0000000..bd9a6f4 --- /dev/null +++ b/workers/main/src/common/errors/OAuth2Error.ts @@ -0,0 +1,10 @@ +import { AppError } from './AppError'; + +export class OAuth2Error extends AppError { + public readonly code: string; + + constructor(message: string, code: string = 'UNKNOWN_OAUTH2_ERROR') { + super(message, 'OAuth2Error'); + this.code = code; + } +} diff --git a/workers/main/src/common/errors/index.ts b/workers/main/src/common/errors/index.ts index 8e8f9d8..1841927 100644 --- a/workers/main/src/common/errors/index.ts +++ b/workers/main/src/common/errors/index.ts @@ -1,6 +1,7 @@ export * from './AppError'; export * from './FileUtilsError'; export * from './FinAppRepositoryError'; +export * from './OAuth2Error'; export * from './QuickBooksRepositoryError'; export * from './SlackRepositoryError'; export * from './TargetUnitRepositoryError';