Skip to content

Commit 0627ebe

Browse files
refactor: simplify error handling - handle errors in runXXX, no rethrow after setFailed
1 parent f58a4da commit 0627ebe

File tree

2 files changed

+70
-98
lines changed

2 files changed

+70
-98
lines changed

dist/main/index.js

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -29157,62 +29157,47 @@ async function runAuth(coreAdapter = core) {
2915729157
coreAdapter.debug(`Key Secret: ${keySecret}`);
2915829158
coreAdapter.debug(`Base URL: ${baseUrl}`);
2915929159
// Authenticate and get the token
29160-
try {
29161-
const authResponse = await axios_1.default.post(`${baseUrl}/api/login`, `grant_type=client_credentials&client_id=${clientId}&client_secret=${keySecret}`, {
29162-
headers: {
29163-
'Content-Type': 'application/x-www-form-urlencoded'
29164-
},
29165-
maxRedirects: 5 // Follow redirects
29166-
});
29167-
const token = authResponse.data.access_token;
29168-
coreAdapter.debug(`Token: ${token}`);
29169-
// Write token and other variables to a temporary file
29170-
const tempDir = process.env.RUNNER_TEMP || os.tmpdir();
29171-
const tokenFilePath = path.join(tempDir, 'meshstack_token.json');
29172-
const tokenData = {
29173-
token,
29174-
baseUrl,
29175-
};
29176-
fs.writeFileSync(tokenFilePath, JSON.stringify(tokenData));
29177-
coreAdapter.debug(`Token file path: ${tokenFilePath}`);
29178-
// Indicate successful login
29179-
coreAdapter.info('Login was successful.');
29180-
// Output the token file path
29181-
coreAdapter.setOutput('token_file', tokenFilePath);
29182-
// Read token from the file
29183-
const fileTokenData = JSON.parse(fs.readFileSync(tokenFilePath, 'utf8'));
29184-
const fileToken = fileTokenData.token;
29185-
}
29186-
catch (authError) {
29187-
if ((0, error_utils_1.isAxiosError)(authError)) {
29188-
(0, error_utils_1.logAxiosError)(authError, coreAdapter, 'Authentication error');
29189-
}
29190-
else {
29191-
coreAdapter.error(`Unexpected error: ${authError}`);
29192-
}
29193-
throw authError;
29194-
}
29160+
const authResponse = await axios_1.default.post(`${baseUrl}/api/login`, `grant_type=client_credentials&client_id=${clientId}&client_secret=${keySecret}`, {
29161+
headers: {
29162+
'Content-Type': 'application/x-www-form-urlencoded'
29163+
},
29164+
maxRedirects: 5 // Follow redirects
29165+
});
29166+
const token = authResponse.data.access_token;
29167+
coreAdapter.debug(`Token: ${token}`);
29168+
// Write token and other variables to a temporary file
29169+
const tempDir = process.env.RUNNER_TEMP || os.tmpdir();
29170+
const tokenFilePath = path.join(tempDir, 'meshstack_token.json');
29171+
const tokenData = {
29172+
token,
29173+
baseUrl,
29174+
};
29175+
fs.writeFileSync(tokenFilePath, JSON.stringify(tokenData));
29176+
coreAdapter.debug(`Token file path: ${tokenFilePath}`);
29177+
// Indicate successful login
29178+
coreAdapter.info('Login was successful.');
29179+
// Output the token file path
29180+
coreAdapter.setOutput('token_file', tokenFilePath);
29181+
// Read token from the file
29182+
const fileTokenData = JSON.parse(fs.readFileSync(tokenFilePath, 'utf8'));
29183+
const fileToken = fileTokenData.token;
2919529184
}
2919629185
catch (error) {
29197-
// Exception handler of last resort
29198-
if (error instanceof Error) {
29199-
coreAdapter.setFailed(error.message);
29186+
// Handle all errors at this level
29187+
if ((0, error_utils_1.isAxiosError)(error)) {
29188+
(0, error_utils_1.logAxiosError)(error, coreAdapter, 'Authentication error');
29189+
}
29190+
else if (error instanceof Error) {
29191+
coreAdapter.error(error.message);
2920029192
}
2920129193
else {
29202-
coreAdapter.setFailed(`An unknown error occurred: ${error}`);
29194+
coreAdapter.error(`Unexpected error: ${error}`);
2920329195
}
29204-
throw error;
29196+
coreAdapter.setFailed(error instanceof Error ? error.message : String(error));
2920529197
}
2920629198
}
2920729199
async function run() {
29208-
try {
29209-
await runAuth(core);
29210-
}
29211-
catch (error) {
29212-
// Last-resort exception handler: prevent unhandled rejections
29213-
// The error has already been logged and setFailed has been called
29214-
process.exit(1);
29215-
}
29200+
await runAuth(core);
2921629201
}
2921729202
// Only run if this file is executed directly (not imported)
2921829203
if (require.main === require.cache[eval('__filename')]) {

src/index.ts

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -32,68 +32,55 @@ export async function runAuth(coreAdapter: CoreAdapter = core) {
3232
coreAdapter.debug(`Base URL: ${baseUrl}`);
3333

3434
// Authenticate and get the token
35-
try {
36-
const authResponse = await axios.post(
37-
`${baseUrl}/api/login`,
38-
`grant_type=client_credentials&client_id=${clientId}&client_secret=${keySecret}`,
39-
{
40-
headers: {
41-
'Content-Type': 'application/x-www-form-urlencoded'
42-
},
43-
maxRedirects: 5 // Follow redirects
44-
}
45-
);
35+
const authResponse = await axios.post(
36+
`${baseUrl}/api/login`,
37+
`grant_type=client_credentials&client_id=${clientId}&client_secret=${keySecret}`,
38+
{
39+
headers: {
40+
'Content-Type': 'application/x-www-form-urlencoded'
41+
},
42+
maxRedirects: 5 // Follow redirects
43+
}
44+
);
4645

47-
const token = authResponse.data.access_token;
48-
coreAdapter.debug(`Token: ${token}`);
46+
const token = authResponse.data.access_token;
47+
coreAdapter.debug(`Token: ${token}`);
4948

50-
// Write token and other variables to a temporary file
51-
const tempDir = process.env.RUNNER_TEMP || os.tmpdir();
52-
const tokenFilePath = path.join(tempDir, 'meshstack_token.json');
53-
const tokenData = {
54-
token,
55-
baseUrl,
56-
};
57-
fs.writeFileSync(tokenFilePath, JSON.stringify(tokenData));
58-
coreAdapter.debug(`Token file path: ${tokenFilePath}`);
49+
// Write token and other variables to a temporary file
50+
const tempDir = process.env.RUNNER_TEMP || os.tmpdir();
51+
const tokenFilePath = path.join(tempDir, 'meshstack_token.json');
52+
const tokenData = {
53+
token,
54+
baseUrl,
55+
};
56+
fs.writeFileSync(tokenFilePath, JSON.stringify(tokenData));
57+
coreAdapter.debug(`Token file path: ${tokenFilePath}`);
5958

60-
// Indicate successful login
61-
coreAdapter.info('Login was successful.');
62-
63-
// Output the token file path
64-
coreAdapter.setOutput('token_file', tokenFilePath);
59+
// Indicate successful login
60+
coreAdapter.info('Login was successful.');
61+
62+
// Output the token file path
63+
coreAdapter.setOutput('token_file', tokenFilePath);
6564

66-
// Read token from the file
67-
const fileTokenData = JSON.parse(fs.readFileSync(tokenFilePath, 'utf8'));
68-
const fileToken = fileTokenData.token;
65+
// Read token from the file
66+
const fileTokenData = JSON.parse(fs.readFileSync(tokenFilePath, 'utf8'));
67+
const fileToken = fileTokenData.token;
6968

70-
} catch (authError) {
71-
if (isAxiosError(authError)) {
72-
logAxiosError(authError, coreAdapter, 'Authentication error');
73-
} else {
74-
coreAdapter.error(`Unexpected error: ${authError}`);
75-
}
76-
throw authError;
77-
}
7869
} catch (error) {
79-
// Exception handler of last resort
80-
if (error instanceof Error) {
81-
coreAdapter.setFailed(error.message);
70+
// Handle all errors at this level
71+
if (isAxiosError(error)) {
72+
logAxiosError(error, coreAdapter, 'Authentication error');
73+
} else if (error instanceof Error) {
74+
coreAdapter.error(error.message);
8275
} else {
83-
coreAdapter.setFailed(`An unknown error occurred: ${error}`);
76+
coreAdapter.error(`Unexpected error: ${error}`);
8477
}
85-
throw error;
78+
coreAdapter.setFailed(error instanceof Error ? error.message : String(error));
8679
}
8780
}
8881

8982
async function run() {
90-
try {
91-
await runAuth(core);
92-
} catch (error) {
93-
// Last-resort exception handler: prevent unhandled rejections
94-
// The error has already been logged and setFailed has been called
95-
process.exit(1);
96-
}
83+
await runAuth(core);
9784
}
9885

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

0 commit comments

Comments
 (0)