Skip to content

Commit fc6c355

Browse files
committed
feat: log API errors in non-production environments
1 parent 5631a68 commit fc6c355

File tree

1 file changed

+60
-13
lines changed

1 file changed

+60
-13
lines changed

src/resend.ts

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,41 @@ export class Resend {
7272
if (!response.ok) {
7373
try {
7474
const rawError = await response.text();
75+
const parsedError = JSON.parse(rawError);
76+
77+
if (process.env.NODE_ENV !== 'production') {
78+
console.error('[Resend API Error]:', {
79+
status: response.status,
80+
error: parsedError,
81+
path,
82+
});
83+
}
84+
7585
return {
7686
data: null,
77-
error: JSON.parse(rawError),
87+
error: parsedError,
7888
headers: Object.fromEntries(response.headers.entries()),
7989
};
8090
} catch (err) {
8191
if (err instanceof SyntaxError) {
92+
const error = {
93+
name: 'application_error',
94+
statusCode: response.status,
95+
message:
96+
'Internal server error. We are unable to process your request right now, please try again later.',
97+
};
98+
99+
if (process.env.NODE_ENV !== 'production') {
100+
console.error('[Resend API Error]:', {
101+
status: response.status,
102+
error,
103+
path,
104+
});
105+
}
106+
82107
return {
83108
data: null,
84-
error: {
85-
name: 'application_error',
86-
statusCode: response.status,
87-
message:
88-
'Internal server error. We are unable to process your request right now, please try again later.',
89-
},
109+
error,
90110
headers: Object.fromEntries(response.headers.entries()),
91111
};
92112
}
@@ -98,13 +118,31 @@ export class Resend {
98118
};
99119

100120
if (err instanceof Error) {
121+
const errorWithMessage = { ...error, message: err.message };
122+
123+
if (process.env.NODE_ENV !== 'production') {
124+
console.error('[Resend API Error]:', {
125+
status: response.status,
126+
error: errorWithMessage,
127+
path,
128+
});
129+
}
130+
101131
return {
102132
data: null,
103-
error: { ...error, message: err.message },
133+
error: errorWithMessage,
104134
headers: Object.fromEntries(response.headers.entries()),
105135
};
106136
}
107137

138+
if (process.env.NODE_ENV !== 'production') {
139+
console.error('[Resend API Error]:', {
140+
status: response.status,
141+
error,
142+
path,
143+
});
144+
}
145+
108146
return {
109147
data: null,
110148
error,
@@ -120,13 +158,22 @@ export class Resend {
120158
headers: Object.fromEntries(response.headers.entries()),
121159
};
122160
} catch {
161+
const error = {
162+
name: 'application_error',
163+
statusCode: null,
164+
message: 'Unable to fetch data. The request could not be resolved.',
165+
};
166+
167+
if (process.env.NODE_ENV !== 'production') {
168+
console.error('[Resend API Error]:', {
169+
error,
170+
path,
171+
});
172+
}
173+
123174
return {
124175
data: null,
125-
error: {
126-
name: 'application_error',
127-
statusCode: null,
128-
message: 'Unable to fetch data. The request could not be resolved.',
129-
},
176+
error,
130177
headers: null,
131178
};
132179
}

0 commit comments

Comments
 (0)