Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(worker): Remove redundant Bridge error handling #6875

Merged
merged 6 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/api/src/app/events/e2e/bridge-trigger.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,11 @@ contexts.forEach((context: Context) => {
expect(messagesAfter.length).to.be.eq(0);
const executionDetailsRequired = await executionDetailsRepository.find({
_environmentId: session.environment._id,
status: ExecutionDetailsStatusEnum.WARNING,
status: ExecutionDetailsStatusEnum.FAILED,
});

let raw = JSON.parse(executionDetailsRequired[0]?.raw ?? '');
let error = raw.raw.data[0].message;
let error = raw.data[0].message;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This raw.raw stutter was unnecessary and made the UI more difficult to read.


expect(error).to.include("must have required property 'name'");

Expand All @@ -344,10 +344,10 @@ contexts.forEach((context: Context) => {

const executionDetailsInvalidType = await executionDetailsRepository.find({
_environmentId: session.environment._id,
status: ExecutionDetailsStatusEnum.WARNING,
status: ExecutionDetailsStatusEnum.FAILED,
});
raw = JSON.parse(executionDetailsInvalidType[0]?.raw ?? '');
error = raw.raw.data[0].message;
error = raw.data[0].message;

expect(error).to.include('must be string');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,102 +190,36 @@ export class ExecuteBridgeJob {
searchParams,
workflowOrigin,
environmentId,
}: Omit<ExecuteBridgeRequestCommand, 'afterResponse' | 'action' | 'retriesLimit'> & {
}: Omit<ExecuteBridgeRequestCommand, 'processError' | 'action' | 'retriesLimit'> & {
job: JobEntity;
}): Promise<ExecuteOutput> {
try {
return this.executeBridgeRequest.execute({
statelessBridgeUrl,
event,
action: PostActionEnum.EXECUTE,
searchParams,
afterResponse: async (response) => {
const body = response?.body as string;

if (response.statusCode >= 400) {
let rawMessage: Record<string, unknown>;
try {
rawMessage = JSON.parse(body);
} catch {
Logger.error(`Unexpected body received from Bridge: ${body}`, LOG_CONTEXT);
rawMessage = {
error: `Unexpected body received from Bridge: ${body}`,
};
}
const createExecutionDetailsCommand: CreateExecutionDetailsCommand = {
...CreateExecutionDetailsCommand.getDetailsFromJob(job),
detail: DetailEnum.FAILED_BRIDGE_RETRY,
source: ExecutionDetailsSourceEnum.INTERNAL,
status: ExecutionDetailsStatusEnum.WARNING,
isTest: false,
isRetry: false,
raw: JSON.stringify({
url: statelessBridgeUrl,
statusCode: response.statusCode,
retryCount: response.retryCount,
message: response.statusMessage,
...(body && body?.length > 0 ? { raw: rawMessage } : {}),
}),
};

await this.createExecutionDetails.execute(createExecutionDetailsCommand);
}

return response;
},
workflowOrigin,
environmentId,
}) as Promise<ExecuteOutput>;
} catch (error: any) {
Logger.error(error, 'Error sending Bridge request:', LOG_CONTEXT);

let raw: { retryCount?: number; statusCode?: number; message: string; url?: string };

if (error.response) {
rifont marked this conversation as resolved.
Show resolved Hide resolved
let rawMessage: Record<string, unknown>;
const errorResponseBody = error?.response?.body;
try {
rawMessage = JSON.parse(errorResponseBody);
} catch {
Logger.error(`Unexpected body received from Bridge: ${errorResponseBody}`, LOG_CONTEXT);
rawMessage = {
error: `Unexpected body received from Bridge: ${errorResponseBody}`,
};
}

raw = {
url: statelessBridgeUrl,
statusCode: error.response?.statusCode,
message: error.response?.statusMessage,
...(error.response?.retryCount ? { retryCount: error.response?.retryCount } : {}),
...(error?.response?.body?.length > 0 ? { raw: rawMessage } : {}),
};
} else if (error.message) {
raw = {
url: statelessBridgeUrl,
message: error.message,
return this.executeBridgeRequest.execute({
statelessBridgeUrl,
event,
action: PostActionEnum.EXECUTE,
searchParams,
processError: async (response) => {
const createExecutionDetailsCommand: CreateExecutionDetailsCommand = {
...CreateExecutionDetailsCommand.getDetailsFromJob(job),
detail: DetailEnum.FAILED_BRIDGE_EXECUTION,
source: ExecutionDetailsSourceEnum.INTERNAL,
status: ExecutionDetailsStatusEnum.FAILED,
isTest: false,
isRetry: false,
raw: JSON.stringify({
url: response.url,
statusCode: response.statusCode,
message: response.message,
code: response.code,
data: response.data,
}),
};
} else {
raw = {
url: statelessBridgeUrl,
message: 'An Unexpected Error Occurred',
};
}

const createExecutionDetailsCommand: CreateExecutionDetailsCommand = {
...CreateExecutionDetailsCommand.getDetailsFromJob(job),
detail: DetailEnum.FAILED_BRIDGE_EXECUTION,
source: ExecutionDetailsSourceEnum.INTERNAL,
status: ExecutionDetailsStatusEnum.FAILED,
isTest: false,
isRetry: false,
raw: JSON.stringify(raw),
};

await this.createExecutionDetails.execute(createExecutionDetailsCommand);

throw error;
}
await this.createExecutionDetails.execute(createExecutionDetailsCommand);
},
workflowOrigin,
environmentId,
}) as Promise<ExecuteOutput>;
}

private async mapState(job: JobEntity, payload: Record<string, unknown>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export enum DetailEnum {
MESSAGE_CONTENT_SYNTAX_ERROR = 'Message content could not be generated due to syntax error in email editor',
MESSAGE_CREATED = 'Message created',
SUCCESSFUL_BRIDGE_RESPONSE_RECEIVED = 'Successful Bridge response received',
FAILED_BRIDGE_RETRY = 'Failed to get response from Bridge, will be retried',
rifont marked this conversation as resolved.
Show resolved Hide resolved
FAILED_BRIDGE_EXECUTION = 'Bridge execution failed',
SUBSCRIBER_NO_ACTIVE_INTEGRATION = 'Subscriber does not have an active integration',
LAYOUT_NOT_FOUND = 'Layout not found ',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { IsDefined, IsOptional } from 'class-validator';
import { AfterResponseHook } from 'got';
import {
CodeResult,
DiscoverOutput,
Expand All @@ -13,6 +12,17 @@ import {
import { WorkflowOriginEnum } from '@novu/shared';
import { EnvironmentLevelCommand } from '../../commands';

export type BridgeError = {
url: string;
code: string;
message: string;
statusCode: number;
data?: unknown;
cause?: unknown;
};

export type ProcessError = (response: BridgeError) => Promise<void>;

export class ExecuteBridgeRequestCommand extends EnvironmentLevelCommand {
@IsOptional()
event?: Omit<Event, `${HttpQueryKeysEnum}`>;
Expand All @@ -21,7 +31,7 @@ export class ExecuteBridgeRequestCommand extends EnvironmentLevelCommand {
searchParams?: Partial<Record<HttpQueryKeysEnum, string>>;

@IsOptional()
afterResponse?: AfterResponseHook;
processError?: ProcessError;

@IsDefined()
action: PostActionEnum | GetActionEnum;
Expand Down
Loading
Loading