Skip to content

Commit 1dc3000

Browse files
refactor: simplify error handling - handle errors in runXXX, no rethrow after setFailed
1 parent 03cae4f commit 1dc3000

File tree

2 files changed

+39
-87
lines changed

2 files changed

+39
-87
lines changed

dist/main/index.js

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33452,21 +33452,10 @@ async function loadBuildingBlockRunFromUrl(url, token, coreAdapter) {
3345233452
'Accept': 'application/vnd.meshcloud.api.meshbuildingblockrun.v1.hal+json',
3345333453
'Authorization': `Bearer ${token}`
3345433454
};
33455-
try {
33456-
const response = await axios_1.default.get(url, { headers });
33457-
const buildingBlockRunJson = response.data;
33458-
coreAdapter.debug(`Fetched Building Block Run: ${JSON.stringify(buildingBlockRunJson)}`);
33459-
return buildingBlockRunJson;
33460-
}
33461-
catch (fetchError) {
33462-
if ((0, error_utils_1.isAxiosError)(fetchError)) {
33463-
(0, error_utils_1.logAxiosError)(fetchError, coreAdapter, 'Failed to fetch building block run');
33464-
}
33465-
else {
33466-
coreAdapter.error(`Unexpected error during fetch: ${fetchError}`);
33467-
}
33468-
throw fetchError;
33469-
}
33455+
const response = await axios_1.default.get(url, { headers });
33456+
const buildingBlockRunJson = response.data;
33457+
coreAdapter.debug(`Fetched Building Block Run: ${JSON.stringify(buildingBlockRunJson)}`);
33458+
return buildingBlockRunJson;
3347033459
}
3347133460
function extractInputs(buildingBlockRun, coreAdapter) {
3347233461
coreAdapter.debug('Extracting inputs from building block run');
@@ -33505,22 +33494,11 @@ function buildRequestPayload(steps, githubContext) {
3350533494
async function registerSource(buildingBlockRunUrl, requestPayload, requestHeaders, tokenFilePath, coreAdapter) {
3350633495
coreAdapter.debug(`Request Payload: ${JSON.stringify(requestPayload)}`);
3350733496
coreAdapter.debug(`Request Headers: ${JSON.stringify(requestHeaders)}`);
33508-
try {
33509-
const response = await axios_1.default.post(`${buildingBlockRunUrl}/status/source`, requestPayload, {
33510-
headers: requestHeaders
33511-
});
33512-
coreAdapter.setOutput('response', response.data);
33513-
coreAdapter.setOutput('token_file', tokenFilePath);
33514-
}
33515-
catch (registerError) {
33516-
if ((0, error_utils_1.isAxiosError)(registerError)) {
33517-
(0, error_utils_1.logAxiosError)(registerError, coreAdapter, 'Failed to register source');
33518-
}
33519-
else {
33520-
coreAdapter.error(`Unexpected error: ${registerError}`);
33521-
}
33522-
throw registerError;
33523-
}
33497+
const response = await axios_1.default.post(`${buildingBlockRunUrl}/status/source`, requestPayload, {
33498+
headers: requestHeaders
33499+
});
33500+
coreAdapter.setOutput('response', response.data);
33501+
coreAdapter.setOutput('token_file', tokenFilePath);
3352433502
}
3352533503
async function runRegisterSource(coreAdapter = core, githubContext = github) {
3352633504
try {
@@ -33557,25 +33535,21 @@ async function runRegisterSource(coreAdapter = core, githubContext = github) {
3355733535
await registerSource(runUrl, requestPayload, requestHeaders, tokenFilePath, coreAdapter);
3355833536
}
3355933537
catch (error) {
33560-
// Exception handler of last resort
33561-
if (error instanceof Error) {
33562-
coreAdapter.setFailed(error.message);
33538+
// Handle all errors at this level
33539+
if ((0, error_utils_1.isAxiosError)(error)) {
33540+
(0, error_utils_1.logAxiosError)(error, coreAdapter, 'Register source operation failed');
33541+
}
33542+
else if (error instanceof Error) {
33543+
coreAdapter.error(error.message);
3356333544
}
3356433545
else {
33565-
coreAdapter.setFailed(`An unknown error occurred: ${error}`);
33546+
coreAdapter.error(`Unexpected error: ${error}`);
3356633547
}
33567-
throw error;
33548+
coreAdapter.setFailed(error instanceof Error ? error.message : String(error));
3356833549
}
3356933550
}
3357033551
async function run() {
33571-
try {
33572-
await runRegisterSource(core, github);
33573-
}
33574-
catch (error) {
33575-
// Last-resort exception handler: prevent unhandled rejections
33576-
// The error has already been logged and setFailed has been called
33577-
process.exit(1);
33578-
}
33552+
await runRegisterSource(core, github);
3357933553
}
3358033554
// Only run if this file is executed directly (not imported)
3358133555
if (require.main === require.cache[eval('__filename')]) {

src/index.ts

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,10 @@ async function loadBuildingBlockRunFromUrl(
137137
'Authorization': `Bearer ${token}`
138138
};
139139

140-
try {
141-
const response = await axios.get(url, { headers });
142-
const buildingBlockRunJson = response.data;
143-
coreAdapter.debug(`Fetched Building Block Run: ${JSON.stringify(buildingBlockRunJson)}`);
144-
return buildingBlockRunJson;
145-
} catch (fetchError) {
146-
if (isAxiosError(fetchError)) {
147-
logAxiosError(fetchError, coreAdapter, 'Failed to fetch building block run');
148-
} else {
149-
coreAdapter.error(`Unexpected error during fetch: ${fetchError}`);
150-
}
151-
throw fetchError;
152-
}
140+
const response = await axios.get(url, { headers });
141+
const buildingBlockRunJson = response.data;
142+
coreAdapter.debug(`Fetched Building Block Run: ${JSON.stringify(buildingBlockRunJson)}`);
143+
return buildingBlockRunJson;
153144
}
154145

155146
function extractInputs(buildingBlockRun: BuildingBlockRun, coreAdapter: CoreAdapter): ExtractedInputs {
@@ -204,25 +195,16 @@ async function registerSource(
204195
coreAdapter.debug(`Request Payload: ${JSON.stringify(requestPayload)}`);
205196
coreAdapter.debug(`Request Headers: ${JSON.stringify(requestHeaders)}`);
206197

207-
try {
208-
const response = await axios.post(
209-
`${buildingBlockRunUrl}/status/source`,
210-
requestPayload,
211-
{
212-
headers: requestHeaders
213-
}
214-
);
215-
216-
coreAdapter.setOutput('response', response.data);
217-
coreAdapter.setOutput('token_file', tokenFilePath);
218-
} catch (registerError) {
219-
if (isAxiosError(registerError)) {
220-
logAxiosError(registerError, coreAdapter, 'Failed to register source');
221-
} else {
222-
coreAdapter.error(`Unexpected error: ${registerError}`);
198+
const response = await axios.post(
199+
`${buildingBlockRunUrl}/status/source`,
200+
requestPayload,
201+
{
202+
headers: requestHeaders
223203
}
224-
throw registerError;
225-
}
204+
);
205+
206+
coreAdapter.setOutput('response', response.data);
207+
coreAdapter.setOutput('token_file', tokenFilePath);
226208
}
227209

228210
export async function runRegisterSource(
@@ -271,24 +253,20 @@ export async function runRegisterSource(
271253
// Register the source
272254
await registerSource(runUrl, requestPayload, requestHeaders, tokenFilePath, coreAdapter);
273255
} catch (error) {
274-
// Exception handler of last resort
275-
if (error instanceof Error) {
276-
coreAdapter.setFailed(error.message);
256+
// Handle all errors at this level
257+
if (isAxiosError(error)) {
258+
logAxiosError(error, coreAdapter, 'Register source operation failed');
259+
} else if (error instanceof Error) {
260+
coreAdapter.error(error.message);
277261
} else {
278-
coreAdapter.setFailed(`An unknown error occurred: ${error}`);
262+
coreAdapter.error(`Unexpected error: ${error}`);
279263
}
280-
throw error;
264+
coreAdapter.setFailed(error instanceof Error ? error.message : String(error));
281265
}
282266
}
283267

284268
async function run() {
285-
try {
286-
await runRegisterSource(core, github);
287-
} catch (error) {
288-
// Last-resort exception handler: prevent unhandled rejections
289-
// The error has already been logged and setFailed has been called
290-
process.exit(1);
291-
}
269+
await runRegisterSource(core, github);
292270
}
293271

294272
// Only run if this file is executed directly (not imported)

0 commit comments

Comments
 (0)