Skip to content

Commit 401471d

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

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

src/resend.ts

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,50 @@ export class Resend {
6565
});
6666
}
6767

68+
private logError(error: ErrorResponse, path: string, status?: number): void {
69+
if (
70+
typeof process !== 'undefined' &&
71+
process.env &&
72+
process.env.NODE_ENV !== 'production'
73+
) {
74+
console.error('[Resend API Error]:', {
75+
...(status !== undefined && { status }),
76+
error,
77+
path,
78+
});
79+
}
80+
}
81+
6882
async fetchRequest<T>(path: string, options = {}): Promise<Response<T>> {
6983
try {
7084
const response = await fetch(`${baseUrl}${path}`, options);
7185

7286
if (!response.ok) {
7387
try {
7488
const rawError = await response.text();
89+
const parsedError = JSON.parse(rawError);
90+
91+
this.logError(parsedError, path, response.status);
92+
7593
return {
7694
data: null,
77-
error: JSON.parse(rawError),
95+
error: parsedError,
7896
headers: Object.fromEntries(response.headers.entries()),
7997
};
8098
} catch (err) {
8199
if (err instanceof SyntaxError) {
100+
const error = {
101+
name: 'application_error',
102+
statusCode: response.status,
103+
message:
104+
'Internal server error. We are unable to process your request right now, please try again later.',
105+
};
106+
107+
this.logError(error, path, response.status);
108+
82109
return {
83110
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-
},
111+
error,
90112
headers: Object.fromEntries(response.headers.entries()),
91113
};
92114
}
@@ -98,13 +120,19 @@ export class Resend {
98120
};
99121

100122
if (err instanceof Error) {
123+
const errorWithMessage = { ...error, message: err.message };
124+
125+
this.logError(errorWithMessage, path, response.status);
126+
101127
return {
102128
data: null,
103-
error: { ...error, message: err.message },
129+
error: errorWithMessage,
104130
headers: Object.fromEntries(response.headers.entries()),
105131
};
106132
}
107133

134+
this.logError(error, path, response.status);
135+
108136
return {
109137
data: null,
110138
error,
@@ -120,13 +148,17 @@ export class Resend {
120148
headers: Object.fromEntries(response.headers.entries()),
121149
};
122150
} catch {
151+
const error = {
152+
name: 'application_error',
153+
statusCode: null,
154+
message: 'Unable to fetch data. The request could not be resolved.',
155+
};
156+
157+
this.logError(error, path);
158+
123159
return {
124160
data: null,
125-
error: {
126-
name: 'application_error',
127-
statusCode: null,
128-
message: 'Unable to fetch data. The request could not be resolved.',
129-
},
161+
error,
130162
headers: null,
131163
};
132164
}

0 commit comments

Comments
 (0)