diff --git a/src/services/idnot/index.ts b/src/services/idnot/index.ts index 537bf28..9ca3f43 100644 --- a/src/services/idnot/index.ts +++ b/src/services/idnot/index.ts @@ -1,5 +1,6 @@ import fetch from 'node-fetch'; import { IdNotUser, ECivility, EOfficeStatus, EIdnotRole } from '../../types'; +import { ValidationError, UnauthorizedError, ExternalServiceError } from '../../types/errors'; export class IdNotService { static async exchangeCodeForTokens(code: string) { @@ -31,7 +32,13 @@ export class IdNotService { }); if (!response.ok) { - throw new Error(`Token exchange failed: ${response.status} ${response.statusText}`); + if (response.status === 400) { + throw new ValidationError('Invalid authorization code received from IdNot'); + } + if (response.status === 401) { + throw new UnauthorizedError('Invalid IdNot client credentials'); + } + throw new ExternalServiceError('IdNot', `Token exchange failed: ${response.status} ${response.statusText}`); } return response.json();