Skip to content

Commit f58a4da

Browse files
refactor: implement proper error handling with concise AxiosError formatting and last-resort exception handlers
1 parent 1e47cff commit f58a4da

File tree

3 files changed

+153
-19
lines changed

3 files changed

+153
-19
lines changed

dist/main/index.js

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29044,6 +29044,69 @@ module.exports = {
2904429044
}
2904529045

2904629046

29047+
/***/ }),
29048+
29049+
/***/ 823:
29050+
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
29051+
29052+
"use strict";
29053+
29054+
var __importDefault = (this && this.__importDefault) || function (mod) {
29055+
return (mod && mod.__esModule) ? mod : { "default": mod };
29056+
};
29057+
Object.defineProperty(exports, "__esModule", ({ value: true }));
29058+
exports.formatAxiosError = formatAxiosError;
29059+
exports.logAxiosError = logAxiosError;
29060+
exports.isAxiosError = isAxiosError;
29061+
const axios_1 = __importDefault(__nccwpck_require__(8757));
29062+
/**
29063+
* Formats an AxiosError into a concise, readable error message
29064+
* Includes: HTTP method, URL, status code, and response body (typically error message)
29065+
* Excludes verbose config details that cause thousands of lines of output
29066+
*/
29067+
function formatAxiosError(error) {
29068+
const parts = [];
29069+
if (error.config) {
29070+
if (error.config.method) {
29071+
parts.push(`Method: ${error.config.method.toUpperCase()}`);
29072+
}
29073+
if (error.config.url) {
29074+
parts.push(`URL: ${error.config.url}`);
29075+
}
29076+
}
29077+
if (error.response) {
29078+
parts.push(`Status: ${error.response.status}`);
29079+
if (error.response.data) {
29080+
const responseData = error.response.data;
29081+
if (typeof responseData === 'string') {
29082+
parts.push(`Response: ${responseData}`);
29083+
}
29084+
else if (typeof responseData === 'object') {
29085+
parts.push(`Response: ${JSON.stringify(responseData)}`);
29086+
}
29087+
}
29088+
}
29089+
else if (error.message) {
29090+
parts.push(`Error: ${error.message}`);
29091+
}
29092+
return parts.join(' | ');
29093+
}
29094+
/**
29095+
* Logs an AxiosError with concise formatting
29096+
* HTTP errors are considered "expected" and are treated as fully handled
29097+
*/
29098+
function logAxiosError(error, coreAdapter, context) {
29099+
const formattedError = formatAxiosError(error);
29100+
coreAdapter.error(`${context}: ${formattedError}`);
29101+
}
29102+
/**
29103+
* Determines if an error is an AxiosError
29104+
*/
29105+
function isAxiosError(error) {
29106+
return axios_1.default.isAxiosError(error);
29107+
}
29108+
29109+
2904729110
/***/ }),
2904829111

2904929112
/***/ 6144:
@@ -29084,6 +29147,7 @@ const axios_1 = __importDefault(__nccwpck_require__(8757));
2908429147
const fs = __importStar(__nccwpck_require__(7147));
2908529148
const path = __importStar(__nccwpck_require__(1017));
2908629149
const os = __importStar(__nccwpck_require__(2037));
29150+
const error_utils_1 = __nccwpck_require__(823);
2908729151
async function runAuth(coreAdapter = core) {
2908829152
try {
2908929153
const clientId = coreAdapter.getInput('client_id');
@@ -29120,14 +29184,8 @@ async function runAuth(coreAdapter = core) {
2912029184
const fileToken = fileTokenData.token;
2912129185
}
2912229186
catch (authError) {
29123-
if (axios_1.default.isAxiosError(authError)) {
29124-
if (authError.response) {
29125-
coreAdapter.error(`Authentication error response: ${JSON.stringify(authError.response.data)}`);
29126-
coreAdapter.error(`Status code: ${authError.response.status}`);
29127-
}
29128-
else {
29129-
coreAdapter.error(`Authentication error message: ${authError.message}`);
29130-
}
29187+
if ((0, error_utils_1.isAxiosError)(authError)) {
29188+
(0, error_utils_1.logAxiosError)(authError, coreAdapter, 'Authentication error');
2913129189
}
2913229190
else {
2913329191
coreAdapter.error(`Unexpected error: ${authError}`);
@@ -29136,12 +29194,25 @@ async function runAuth(coreAdapter = core) {
2913629194
}
2913729195
}
2913829196
catch (error) {
29139-
coreAdapter.setFailed(`Action failed with error: ${error}`);
29197+
// Exception handler of last resort
29198+
if (error instanceof Error) {
29199+
coreAdapter.setFailed(error.message);
29200+
}
29201+
else {
29202+
coreAdapter.setFailed(`An unknown error occurred: ${error}`);
29203+
}
2914029204
throw error;
2914129205
}
2914229206
}
2914329207
async function run() {
29144-
await runAuth(core);
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+
}
2914529216
}
2914629217
// Only run if this file is executed directly (not imported)
2914729218
if (require.main === require.cache[eval('__filename')]) {

src/error-utils.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import axios, { AxiosError } from 'axios';
2+
3+
export interface CoreAdapter {
4+
error: (message: string) => void;
5+
debug: (message: string) => void;
6+
}
7+
8+
/**
9+
* Formats an AxiosError into a concise, readable error message
10+
* Includes: HTTP method, URL, status code, and response body (typically error message)
11+
* Excludes verbose config details that cause thousands of lines of output
12+
*/
13+
export function formatAxiosError(error: AxiosError): string {
14+
const parts: string[] = [];
15+
16+
if (error.config) {
17+
if (error.config.method) {
18+
parts.push(`Method: ${error.config.method.toUpperCase()}`);
19+
}
20+
if (error.config.url) {
21+
parts.push(`URL: ${error.config.url}`);
22+
}
23+
}
24+
25+
if (error.response) {
26+
parts.push(`Status: ${error.response.status}`);
27+
if (error.response.data) {
28+
const responseData = error.response.data;
29+
if (typeof responseData === 'string') {
30+
parts.push(`Response: ${responseData}`);
31+
} else if (typeof responseData === 'object') {
32+
parts.push(`Response: ${JSON.stringify(responseData)}`);
33+
}
34+
}
35+
} else if (error.message) {
36+
parts.push(`Error: ${error.message}`);
37+
}
38+
39+
return parts.join(' | ');
40+
}
41+
42+
/**
43+
* Logs an AxiosError with concise formatting
44+
* HTTP errors are considered "expected" and are treated as fully handled
45+
*/
46+
export function logAxiosError(error: AxiosError, coreAdapter: CoreAdapter, context: string): void {
47+
const formattedError = formatAxiosError(error);
48+
coreAdapter.error(`${context}: ${formattedError}`);
49+
}
50+
51+
/**
52+
* Determines if an error is an AxiosError
53+
*/
54+
export function isAxiosError(error: any): error is AxiosError {
55+
return axios.isAxiosError(error);
56+
}

src/index.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import axios from 'axios';
33
import * as fs from 'fs';
44
import * as path from 'path';
55
import * as os from 'os';
6+
import { logAxiosError, isAxiosError } from './error-utils';
67

78
export interface AuthInputs {
89
client_id: string;
@@ -67,26 +68,32 @@ export async function runAuth(coreAdapter: CoreAdapter = core) {
6768
const fileToken = fileTokenData.token;
6869

6970
} catch (authError) {
70-
if (axios.isAxiosError(authError)) {
71-
if (authError.response) {
72-
coreAdapter.error(`Authentication error response: ${JSON.stringify(authError.response.data)}`);
73-
coreAdapter.error(`Status code: ${authError.response.status}`);
74-
} else {
75-
coreAdapter.error(`Authentication error message: ${authError.message}`);
76-
}
71+
if (isAxiosError(authError)) {
72+
logAxiosError(authError, coreAdapter, 'Authentication error');
7773
} else {
7874
coreAdapter.error(`Unexpected error: ${authError}`);
7975
}
8076
throw authError;
8177
}
8278
} catch (error) {
83-
coreAdapter.setFailed(`Action failed with error: ${error}`);
79+
// Exception handler of last resort
80+
if (error instanceof Error) {
81+
coreAdapter.setFailed(error.message);
82+
} else {
83+
coreAdapter.setFailed(`An unknown error occurred: ${error}`);
84+
}
8485
throw error;
8586
}
8687
}
8788

8889
async function run() {
89-
await runAuth(core);
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+
}
9097
}
9198

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

0 commit comments

Comments
 (0)