Skip to content

Commit

Permalink
feat: add retry to sendTestPayload
Browse files Browse the repository at this point in the history
Co-authored-by: Carlos <carlos@snyk.io>
  • Loading branch information
xzhou-snyk and carlos-snyk committed Apr 3, 2023
1 parent 806f302 commit 0cc0344
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
14 changes: 14 additions & 0 deletions src/lib/errors/bad-gateway-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { CustomError } from './custom-error';

export class BadGatewayError extends CustomError {
private static ERROR_CODE = 502;
private static ERROR_STRING_CODE = 'BAD_GATEWAY_ERROR';
private static ERROR_MESSAGE = 'Bad gateway error';

constructor(userMessage) {
super(BadGatewayError.ERROR_MESSAGE);
this.code = BadGatewayError.ERROR_CODE;
this.strCode = BadGatewayError.ERROR_STRING_CODE;
this.userMessage = userMessage || BadGatewayError.ERROR_MESSAGE;
}
}
2 changes: 2 additions & 0 deletions src/lib/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export { ConnectionTimeoutError } from './connection-timeout-error';
export { FailedToLoadPolicyError } from './failed-to-load-policy-error';
export { PolicyNotFoundError } from './policy-not-found-error';
export { InternalServerError } from './internal-server-error';
export { BadGatewayError } from './bad-gateway-error';
export { ServiceUnavailableError } from './service-unavailable-error';
export { FailedToGetVulnerabilitiesError } from './failed-to-get-vulnerabilities-error';
export { FailedToGetVulnsFromUnavailableResource } from './failed-to-get-vulns-from-unavailable-resource';
export { UnsupportedPackageManagerError } from './unsupported-package-manager-error';
Expand Down
14 changes: 14 additions & 0 deletions src/lib/errors/service-unavailable-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { CustomError } from './custom-error';

export class ServiceUnavailableError extends CustomError {
private static ERROR_CODE = 503;
private static ERROR_STRING_CODE = 'SERVICE_UNAVAILABLE_ERROR';
private static ERROR_MESSAGE = 'Service unavailable error';

constructor(userMessage) {
super(ServiceUnavailableError.ERROR_MESSAGE);
this.code = ServiceUnavailableError.ERROR_CODE;
this.strCode = ServiceUnavailableError.ERROR_STRING_CODE;
this.userMessage = userMessage || ServiceUnavailableError.ERROR_MESSAGE;
}
}
43 changes: 38 additions & 5 deletions src/lib/snyk-test/run-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {
NoSupportedManifestsFoundError,
NotFoundError,
errorMessageWithRetry,
BadGatewayError,
ServiceUnavailableError,
} from '../errors';
import * as snyk from '../';
import { isCI } from '../is-ci';
Expand Down Expand Up @@ -68,6 +70,7 @@ import { assembleEcosystemPayloads } from './assemble-payloads';
import { makeRequest } from '../request';
import { spinner } from '../spinner';
import { hasUnknownVersions } from '../dep-graph';
import { sleep } from '../common';

const debug = debugModule('snyk:run-test');

Expand Down Expand Up @@ -234,13 +237,35 @@ async function sendAndParseResults(
response: any;
};
const requests: (() => Promise<TestResponse>)[] = [];
for (const payload of payloads) {
for (const originalPayload of payloads) {
const request = async (): Promise<TestResponse> => {
/** sendTestPayload() deletes the request.body from the payload once completed. */
const originalPayload = Object.assign({}, payload);
const response = await sendTestPayload(payload);
return { payload, originalPayload, response };
let remainingAttempts = 3;
let error;

while (remainingAttempts-- > 0) {
debug('sendTestPayload remaining attempts:', remainingAttempts);
try {
/** sendTestPayload() deletes the request.body from the payload once completed. */
const payload = Object.assign({}, originalPayload);
const response = await sendTestPayload(payload);
return { payload, originalPayload, response };
} catch (err) {
error = err;
if (
err instanceof InternalServerError ||
err instanceof BadGatewayError ||
err instanceof ServiceUnavailableError
) {
await sleep(500);
} else {
break;
}
}
}

throw error;
};

requests.push(request);
}

Expand Down Expand Up @@ -516,6 +541,14 @@ function handleTestHttpErrorResponse(res, body) {
err = new InternalServerError(userMessage);
err.innerError = body.stack;
break;
case 502:
err = new BadGatewayError(userMessage);
err.innerError = body.stack;
break;
case 503:
err = new ServiceUnavailableError(userMessage);
err.innerError = body.stack;
break;
default:
err = new FailedToGetVulnerabilitiesError(userMessage, statusCode);
err.innerError = body.error;
Expand Down

0 comments on commit 0cc0344

Please sign in to comment.