Skip to content

Commit 2f5d297

Browse files
committed
Refactor BackendResultInfoDto to separate success and error subclasses
1 parent 6c8c903 commit 2f5d297

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { IsEnum, IsObject, IsOptional, IsString } from 'class-validator';
2-
import { BackendCodesEnum } from '../_interfaces/backend-codes.enum';
2+
import { BackendCodesEnumError, BackendCodesEnumSuccess } from '../_interfaces/backend-codes.enum';
33

44
export class BackendResultInfoDto {
5-
@IsEnum(BackendCodesEnum)
6-
public status: number;
7-
85
@IsString()
96
@IsOptional()
107
public message?: string;
@@ -13,3 +10,13 @@ export class BackendResultInfoDto {
1310
@IsOptional()
1411
public data?: object;
1512
}
13+
14+
export class BackendResultInfoSuccessDto extends BackendResultInfoDto {
15+
@IsEnum(BackendCodesEnumSuccess)
16+
public status: number;
17+
}
18+
19+
export class BackendResultInfoErrorDto extends BackendResultInfoDto {
20+
@IsEnum(BackendCodesEnumError)
21+
public status: number;
22+
}

src/backend-runner/_executors/catch-all.executor.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { join } from 'path';
55
import { Job } from 'bullmq';
66
import { BackendConfigV1Dto } from '../_dto/backend-config-v1.dto';
77
import { BackendResultInfoInterface, BackendResultInterface } from '../_interfaces/backend-result.interface';
8-
import { BackendCodesEnum } from '../_interfaces/backend-codes.enum';
98
import { ValidationError, validateOrReject } from 'class-validator';
10-
import { BackendResultInfoDto } from '../_dto/backend-result-info.dto';
9+
import { BackendResultInfoErrorDto, BackendResultInfoSuccessDto } from '../_dto/backend-result-info.dto';
1110
import { plainToInstance } from 'class-transformer';
11+
import { BackendCodesEnumError } from '../_interfaces/backend-codes.enum';
1212

1313
interface ValidationRecursive {
1414
[key: string]: string;
@@ -58,7 +58,7 @@ export class CatchAllExecutor implements ExecutorInterface {
5858
if (process.status !== 0) {
5959
this.service.logger.error(`Error executing backend ${backend.name}`);
6060
const error = this.extractLastJsonImproved(process.error || process.output);
61-
const errorSchema = plainToInstance(BackendResultInfoDto, error);
61+
const errorSchema = plainToInstance(BackendResultInfoErrorDto, error);
6262
await validateOrReject(errorSchema);
6363

6464
return {
@@ -70,7 +70,7 @@ export class CatchAllExecutor implements ExecutorInterface {
7070

7171
this.service.logger.log(`Backend ${backend.name} executed successfully`);
7272
const output = this.extractLastJsonImproved(process.output);
73-
const outputSchema = plainToInstance(BackendResultInfoDto, output);
73+
const outputSchema = plainToInstance(BackendResultInfoSuccessDto, output);
7474
await validateOrReject(outputSchema);
7575

7676
return {
@@ -89,10 +89,10 @@ export class CatchAllExecutor implements ExecutorInterface {
8989

9090
return {
9191
backend: backend.name,
92-
status: BackendCodesEnum.INVALID_JSON_RESPONSE,
92+
status: BackendCodesEnumError.INVALID_JSON_RESPONSE,
9393
message: `Erreur de validation : ${Object.keys(validations).join(', ')}`.trim(),
9494
error: {
95-
status: BackendCodesEnum.INVALID_JSON_RESPONSE,
95+
status: BackendCodesEnumError.INVALID_JSON_RESPONSE,
9696
message: `Erreur de validation : ${Object.keys(validations).join(', ')}`.trim(),
9797
data: validations,
9898
},
@@ -102,7 +102,7 @@ export class CatchAllExecutor implements ExecutorInterface {
102102

103103
private extractLastJsonImproved(stdout: string): BackendResultInfoInterface {
104104
if (!stdout) return {
105-
status: BackendCodesEnum.INVALID_JSON_RESPONSE,
105+
status: BackendCodesEnumError.INVALID_JSON_RESPONSE,
106106
message: 'No output',
107107
}
108108

@@ -138,7 +138,7 @@ export class CatchAllExecutor implements ExecutorInterface {
138138
}
139139

140140
if (jsonCandidates.length === 0) return {
141-
status: BackendCodesEnum.INVALID_JSON_RESPONSE,
141+
status: BackendCodesEnumError.INVALID_JSON_RESPONSE,
142142
message: 'No JSON output',
143143
}
144144

src/backend-runner/_interfaces/backend-codes.enum.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
* @see https://www.linuxdoc.org/LDP/abs/html/exitcodes.html
77
* @see https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html
88
*/
9-
export enum BackendCodesEnum {
10-
OK = 0,
9+
export enum BackendCodesEnumError {
1110
GENERIC_ERROR = 1 << 0,
1211
GENERIC_STOPPED = 1 << 1,
1312
CONNECTION_ERROR = 1 << 2,
1413
INVALID_LOGIN = 1 << 3,
1514
INVALID_CREDENTIALS = 1 << 4,
1615
INVALID_JSON_RESPONSE = 1 << 5,
1716
}
17+
18+
export enum BackendCodesEnumSuccess {
19+
OK = 0,
20+
}

0 commit comments

Comments
 (0)