forked from Shared/esg
feat: add Persian error translations and comprehensive test suite
Implement normalized error handling with Persian translations across all exception types, replace legacy NestJS exceptions with AppException, and add unit, integration, and smoke tests. - Add error catalog with gateway-owned codes and Persian messages - Introduce AppException wrapping normalized error envelopes - Add translateError helper for automatic messageFa population - Remove claims module and update provider error normalization - Add unit tests for error contracts and helper functions - Add integration tests for admin and inquiry endpoints - Add smoke tests for real provider connectivity - Add test support utilities (auth mocks, assertions, app factory)
This commit is contained in:
@@ -8,6 +8,10 @@ import {
|
||||
import { InquiryType } from '../../common/enums/inquiry-type.enum';
|
||||
import { ProviderName } from '../../common/enums/provider-name.enum';
|
||||
import { NormalizedErrorDto } from '../../common/dto/normalized-error.dto';
|
||||
import {
|
||||
AppErrorCode,
|
||||
buildNormalizedError,
|
||||
} from '../../common/constants/error-messages';
|
||||
import { AmitisAuthServiceConfig } from '../../config/configuration';
|
||||
import { GeneralTokenDocument } from '../schemas/general-token.schema';
|
||||
import { GeneralTokenService } from '../services/general-token.service';
|
||||
@@ -209,14 +213,14 @@ export class AmitisProvider {
|
||||
loginStatus === 2
|
||||
? 'AMITIS username or password is invalid for this service account'
|
||||
: `AMITIS login rejected (LoginStatus=${loginStatus ?? 'unknown'})`,
|
||||
loginStatus === 2 ? 'AUTH_INVALID_CREDENTIALS' : 'AUTH_REJECTED',
|
||||
loginStatus === 2 ? 'PROVIDER_AUTH_FAILED' : 'PROVIDER_REJECTED',
|
||||
);
|
||||
}
|
||||
|
||||
if (loginStatus !== undefined && loginStatus !== 1 && body.IsSucceed !== true) {
|
||||
throw this.createAuthError(
|
||||
`AMITIS login rejected (LoginStatus=${loginStatus})`,
|
||||
'AUTH_REJECTED',
|
||||
'PROVIDER_REJECTED',
|
||||
);
|
||||
}
|
||||
|
||||
@@ -267,13 +271,28 @@ export class AmitisProvider {
|
||||
return `${ProviderName.AMITIS}:${providerName}:${inquiryType}`;
|
||||
}
|
||||
|
||||
private createAuthError(message: string, code: string): Error {
|
||||
const normalized: NormalizedErrorDto = {
|
||||
code,
|
||||
private createAuthError(message: string, code: AppErrorCode): Error {
|
||||
const normalized = buildNormalizedError(code, {
|
||||
message,
|
||||
providerMessage: message,
|
||||
providerCode: code,
|
||||
};
|
||||
});
|
||||
const err = new Error(message);
|
||||
(err as Error & { normalizedError: NormalizedErrorDto }).normalizedError = normalized;
|
||||
return err;
|
||||
}
|
||||
|
||||
private createProviderAuthError(message: string, error: unknown): Error {
|
||||
const providerCode =
|
||||
error instanceof AxiosError
|
||||
? String(error.response?.status ?? error.code ?? 'NETWORK_ERROR')
|
||||
: undefined;
|
||||
const providerMessage = error instanceof Error ? error.message : String(error);
|
||||
const normalized = buildNormalizedError('PROVIDER_AUTH_FAILED', {
|
||||
message,
|
||||
providerMessage,
|
||||
providerCode,
|
||||
});
|
||||
const err = new Error(message);
|
||||
(err as Error & { normalizedError: NormalizedErrorDto }).normalizedError = normalized;
|
||||
return err;
|
||||
@@ -290,16 +309,17 @@ export class AmitisProvider {
|
||||
error.response?.data !== undefined
|
||||
? JSON.stringify(error.response.data).slice(0, 500)
|
||||
: undefined;
|
||||
return new Error(
|
||||
return this.createProviderAuthError(
|
||||
body ? `${message}: ${status} ${error.message} | body=${body}` : `${message}: ${status} ${error.message}`,
|
||||
error,
|
||||
);
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return new Error(`${message}: ${error.message}`);
|
||||
return this.createProviderAuthError(`${message}: ${error.message}`, error);
|
||||
}
|
||||
|
||||
return new Error(`${message}: ${String(error)}`);
|
||||
return this.createProviderAuthError(`${message}: ${String(error)}`, error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user