Skip to content

Commit 94ce150

Browse files
improve fetchForeignAPI zod error logging
1 parent 81149eb commit 94ce150

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/lib/apis/fetchForeignAPI.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Logger } from '../logger';
99
* @param zodSchema The [Zod](https://github.com/colinhacks/zod) schema that the returned data conforms to. The promise will reject if the returned data does not conform to the schema provided.
1010
* @returns A promise that resolves to the expected type or rejects with an [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error).
1111
*/
12-
export const fetchForeignAPI = async <ReturnType = unknown>(request: Request, zodSchema: ZodSchema<ReturnType>): Promise<ReturnType> => {
12+
export const fetchForeignAPI = async <ReturnType = unknown>(request: Request, zodSchema: ZodSchema<ReturnType>, debug?: boolean): Promise<ReturnType> => {
1313
let response: Response;
1414
try {
1515
response = await fetch(request);
@@ -31,9 +31,14 @@ export const fetchForeignAPI = async <ReturnType = unknown>(request: Request, zo
3131
const result = zodSchema.safeParse(data);
3232

3333
if (!result.success) {
34-
Logger.error('[zod] Data validation failed:');
35-
console.log(data); // winston doesn't correctly print object at the moment
36-
result.error.issues.forEach((issue) => Logger.error(`Code: ${issue.code}, Path: ${issue.path.join('.')}, Message: ${issue.message}`));
34+
Logger.error("[zod] Data validation failed! Pass the 'debug' flag to 'fetchForeignAPI()' to print the retrieved data to the console.");
35+
Logger.error(`Endpoint location: ${request.url}.`);
36+
if (debug) {
37+
// winston doesn't usefully log object at the moment
38+
// eslint-disable-next-line no-console
39+
console.log('RETRIEVED DATA:', data);
40+
}
41+
result.error.issues.forEach((issue) => Logger.error(`[zod] Code: ${issue.code}, Path: ${issue.path.join('.')}, Message: ${issue.message}`));
3742
throw result.error;
3843
}
3944

0 commit comments

Comments
 (0)