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

Handle errors better #1713

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/spotty-ducks-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@roadiehq/scaffolder-backend-module-http-request': patch
---

Respect continueOnBadResponse for any error thrown by request and ensure we log the error
Original file line number Diff line number Diff line change
Expand Up @@ -139,60 +139,70 @@ export function createHttpBackstageAction(options: {
);
}

const queryParams: string = params
? new URLSearchParams(params).toString()
: '';
try {
const queryParams: string = params
? new URLSearchParams(params).toString()
: '';

let inputBody: Body = undefined;
let inputBody: Body = undefined;

if (
input.body &&
typeof input.body !== 'string' &&
input.headers &&
input.headers['content-type'] &&
input.headers['content-type'].includes('application/json')
) {
inputBody = JSON.stringify(input.body);
} else {
inputBody = input.body;
}
if (
input.body &&
typeof input.body !== 'string' &&
input.headers &&
input.headers['content-type'] &&
input.headers['content-type'].includes('application/json')
) {
inputBody = JSON.stringify(input.body);
} else {
inputBody = input.body;
}

const httpOptions: HttpOptions = {
method: input.method,
url: queryParams !== '' ? `${url}?${queryParams}` : url,
headers: input.headers ? (input.headers as Headers) : {},
body: inputBody,
};
const httpOptions: HttpOptions = {
method: input.method,
url: queryParams !== '' ? `${url}?${queryParams}` : url,
headers: input.headers ? (input.headers as Headers) : {},
body: inputBody,
};

const authToken = getObjFieldCaseInsensitively(
input.headers,
'authorization',
);
const authToken = getObjFieldCaseInsensitively(
input.headers,
'authorization',
);

if (token && !authToken) {
ctx.logger.info(`Token is defined. Setting authorization header.`);
httpOptions.headers.authorization = `Bearer ${token}`;
}
if (token && !authToken) {
ctx.logger.info(`Token is defined. Setting authorization header.`);
httpOptions.headers.authorization = `Bearer ${token}`;
}

const dryRunSafeMethods = new Set(['GET', 'HEAD', 'OPTIONS']);
if (ctx.isDryRun === true && !dryRunSafeMethods.has(method)) {
ctx.logger.info(
`Dry run mode. Skipping non dry-run safe method '${method}' request to ${
queryParams !== '' ? `${input.path}?${queryParams}` : input.path
}`,
const dryRunSafeMethods = new Set(['GET', 'HEAD', 'OPTIONS']);
if (ctx.isDryRun === true && !dryRunSafeMethods.has(method)) {
ctx.logger.info(
`Dry run mode. Skipping non dry-run safe method '${method}' request to ${
queryParams !== '' ? `${input.path}?${queryParams}` : input.path
}`,
);
return;
}

const { code, headers, body } = await http(
httpOptions,
ctx.logger,
continueOnBadResponse,
);
return;
ctx.output('code', code);
ctx.output('headers', headers);
ctx.output('body', body);
} catch (e: any) {
ctx.logger.error(`Action failed due to unhandled error: ${e?.message}`);
if (continueOnBadResponse) {
ctx.output('code', 500);
ctx.output('headers', {});
ctx.output('body', { error: e?.message });
} else {
throw e;
}
}

const { code, headers, body } = await http(
httpOptions,
ctx.logger,
continueOnBadResponse,
);

ctx.output('code', code);
ctx.output('headers', headers);
ctx.output('body', body);
},
});
}
Loading