Skip to content

Commit

Permalink
Catch responseHandler errors (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgachnang authored Jan 4, 2024
1 parent f91617d commit 3f80d6a
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,15 @@ export function fernsRouter<T>(
});
}
}
const serialized = await responseHandler(data, "create", req, options);
return res.status(201).json({data: serialized});
try {
const serialized = await responseHandler(data, "create", req, options);
return res.status(201).json({data: serialized});
} catch (e: any) {
throw new APIError({
title: `responseHandler error: ${e.message}`,
error: e,
});
}
})
);

Expand Down Expand Up @@ -504,7 +511,16 @@ export function fernsRouter<T>(
// Uses metadata rather than counting the number of documents in the array for performance.
const total = await model.estimatedDocumentCount();

let serialized = await responseHandler(data, "list", req, options);
let serialized;

try {
serialized = await responseHandler(data, "list", req, options);
} catch (e: any) {
throw new APIError({
title: `responseHandler error: ${e.message}`,
error: e,
});
}

let more;
try {
Expand Down Expand Up @@ -579,8 +595,15 @@ export function fernsRouter<T>(
}
}

const serialized = await responseHandler(data, "read", req, options);
return res.json({data: serialized});
try {
const serialized = await responseHandler(data, "read", req, options);
return res.json({data: serialized});
} catch (e: any) {
throw new APIError({
title: `responseHandler error: ${e.message}`,
error: e,
});
}
})
);

Expand Down Expand Up @@ -696,8 +719,16 @@ export function fernsRouter<T>(
});
}
}
const serialized = await responseHandler(doc, "update", req, options);
return res.json({data: serialized});

try {
const serialized = await responseHandler(doc, "update", req, options);
return res.json({data: serialized});
} catch (e: any) {
throw new APIError({
title: `responseHandler error: ${e.message}`,
error: e,
});
}
})
);

Expand Down

0 comments on commit 3f80d6a

Please sign in to comment.