Skip to content

Commit

Permalink
refactor: use discrete if statements per error for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
rifont committed Oct 17, 2024
1 parent 5a6c1f6 commit d0f7987
Showing 1 changed file with 49 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,30 +258,25 @@ export class ExecuteBridgeRequest {
) {
// Handle known Bridge errors. Propagate the error code and message.
throw new HttpException(body, error.response.statusCode);
} else if (body.code === TUNNEL_ERROR_CODE) {
// Handle known tunnel errors
const tunnelBody = body as TunnelResponseError;
Logger.error(
`Could not establish tunnel connection for \`${url}\`. Error: \`${tunnelBody.message}\``,
LOG_CONTEXT,
);
throw new NotFoundException({
message: BRIDGE_EXECUTION_ERROR.TUNNEL_NOT_FOUND.message(url),
code: BRIDGE_EXECUTION_ERROR.TUNNEL_NOT_FOUND.code,
});
} else if (error instanceof TimeoutError) {
}

if (error instanceof TimeoutError) {
Logger.error(`Bridge request timeout for \`${url}\``, LOG_CONTEXT);
throw new GatewayTimeoutException({
message: BRIDGE_EXECUTION_ERROR.BRIDGE_REQUEST_TIMEOUT.message(url),
code: BRIDGE_EXECUTION_ERROR.BRIDGE_REQUEST_TIMEOUT.code,
});
} else if (error instanceof UnsupportedProtocolError) {
}

if (error instanceof UnsupportedProtocolError) {
Logger.error(`Unsupported protocol for \`${url}\``, LOG_CONTEXT);
throw new BadRequestException({
message: BRIDGE_EXECUTION_ERROR.UNSUPPORTED_PROTOCOL.message(url),
code: BRIDGE_EXECUTION_ERROR.UNSUPPORTED_PROTOCOL.code,
});
} else if (error instanceof ReadError) {
}

if (error instanceof ReadError) {
Logger.error(
`Response body could not be read for \`${url}\``,
LOG_CONTEXT,
Expand All @@ -290,7 +285,9 @@ export class ExecuteBridgeRequest {
message: BRIDGE_EXECUTION_ERROR.RESPONSE_READ_ERROR.message(url),
code: BRIDGE_EXECUTION_ERROR.RESPONSE_READ_ERROR.code,
});
} else if (error instanceof UploadError) {
}

if (error instanceof UploadError) {
Logger.error(
`Error uploading request body for \`${url}\``,
LOG_CONTEXT,
Expand All @@ -299,20 +296,39 @@ export class ExecuteBridgeRequest {
message: BRIDGE_EXECUTION_ERROR.REQUEST_UPLOAD_ERROR.message(url),
code: BRIDGE_EXECUTION_ERROR.REQUEST_UPLOAD_ERROR.code,
});
} else if (error instanceof CacheError) {
}

if (error instanceof CacheError) {
Logger.error(`Error caching request for \`${url}\``, LOG_CONTEXT);
throw new BadRequestException({
message: BRIDGE_EXECUTION_ERROR.REQUEST_CACHE_ERROR.message(url),
code: BRIDGE_EXECUTION_ERROR.REQUEST_CACHE_ERROR.code,
});
} else if (error instanceof MaxRedirectsError) {
}

if (error instanceof MaxRedirectsError) {
Logger.error(`Maximum redirects exceeded for \`${url}\``, LOG_CONTEXT);
throw new BadRequestException({
message:
BRIDGE_EXECUTION_ERROR.MAXIMUM_REDIRECTS_EXCEEDED.message(url),
code: BRIDGE_EXECUTION_ERROR.MAXIMUM_REDIRECTS_EXCEEDED.code,
});
} else if (error.response?.statusCode === 502) {
}

if (body.code === TUNNEL_ERROR_CODE) {
// Handle known tunnel errors
const tunnelBody = body as TunnelResponseError;
Logger.error(
`Could not establish tunnel connection for \`${url}\`. Error: \`${tunnelBody.message}\``,
LOG_CONTEXT,
);
throw new NotFoundException({
message: BRIDGE_EXECUTION_ERROR.TUNNEL_NOT_FOUND.message(url),
code: BRIDGE_EXECUTION_ERROR.TUNNEL_NOT_FOUND.code,
});
}

if (error.response?.statusCode === 502) {
/*
* Tunnel was live, but the Bridge endpoint was down.
* 502 is thrown by the tunnel service when the Bridge endpoint is not reachable.
Expand All @@ -326,7 +342,9 @@ export class ExecuteBridgeRequest {
BRIDGE_EXECUTION_ERROR.BRIDGE_ENDPOINT_NOT_FOUND.message(url),
code: BRIDGE_EXECUTION_ERROR.BRIDGE_ENDPOINT_NOT_FOUND.code,
});
} else if (
}

if (
error.response?.statusCode === 404 ||
RETRYABLE_ERROR_CODES.includes(error.code)
) {
Expand All @@ -343,7 +361,9 @@ export class ExecuteBridgeRequest {
BRIDGE_EXECUTION_ERROR.BRIDGE_ENDPOINT_UNAVAILABLE.message(url),
code: codeToThrow,
});
} else if (error.response?.statusCode === 405) {
}

if (error.response?.statusCode === 405) {
Logger.error(
`Bridge endpoint method not configured for \`${url}\``,
LOG_CONTEXT,
Expand All @@ -353,16 +373,16 @@ export class ExecuteBridgeRequest {
BRIDGE_EXECUTION_ERROR.BRIDGE_METHOD_NOT_CONFIGURED.message(url),
code: BRIDGE_EXECUTION_ERROR.BRIDGE_METHOD_NOT_CONFIGURED.code,
});
} else {
Logger.error(
`Unknown bridge request error calling \`${url}\`: \`${JSON.stringify(
body,
)}\``,
error,
LOG_CONTEXT,
);
throw error;
}

Logger.error(
`Unknown bridge request error calling \`${url}\`: \`${JSON.stringify(
body,
)}\``,
error,
LOG_CONTEXT,
);
throw error;
} else {
Logger.error(
`Unknown bridge non-request error calling \`${url}\``,
Expand Down

0 comments on commit d0f7987

Please sign in to comment.