Skip to content

Commit ec84f1d

Browse files
fix: refine error message extraction in axios-logger to explicitly handle null and undefined values
1 parent 8f35cc8 commit ec84f1d

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/lib/axios-logger.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,13 @@ function extractErrorMessage(data: unknown, defaultMessage: string): string {
123123
return String(data);
124124
}
125125

126-
// Fall back to default message for null/undefined
127-
if (!data || typeof data !== "object") {
126+
// Fall back to default message for null/undefined or non-objects
127+
// Note: null is typeof "object" in JavaScript, so we check it explicitly
128+
if (data === null || data === undefined || typeof data !== "object") {
128129
return defaultMessage;
129130
}
130131

132+
// At this point, TypeScript knows data is a non-null object
131133
// error is in `data.error`
132134
if ("error" in data && data.error) {
133135
return String(data.error);
@@ -137,6 +139,11 @@ function extractErrorMessage(data: unknown, defaultMessage: string): string {
137139
if ("errors" in data && data.errors) {
138140
const { errors } = data;
139141

142+
// errors is a string
143+
if (typeof errors === "string") {
144+
return errors;
145+
}
146+
140147
// errors is an array of strings
141148
if (Array.isArray(errors) && errors.length > 0) {
142149
if (typeof errors[0] === "string") {

0 commit comments

Comments
 (0)