From 520c7a3acf2b5e073942a45ee9e75ec384f4732d Mon Sep 17 00:00:00 2001 From: Ayush Kumar <131751122+Ayush277@users.noreply.github.com> Date: Sat, 26 Oct 2024 22:38:20 +0530 Subject: [PATCH] Update wakatime.js This change optimizes error handling by setting a shorter cache duration for errors, ensuring they are cached briefly and revalidated quickly. Using a helper function, setCacheControlHeaders, streamlines cache control setup, making it consistent for both success and error responses. Additionally, renderResponse standardizes error formatting with a 500 status code, ensuring user-friendly error messages and clearer debugging for developers. This enhancement improves reliability and reduces stale error data. --- api/wakatime.js | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/api/wakatime.js b/api/wakatime.js index 73ef9986feeb7..236727fc4e5dc 100644 --- a/api/wakatime.js +++ b/api/wakatime.js @@ -90,20 +90,11 @@ export default async (req, res) => { }), ); } catch (err) { - res.setHeader( - "Cache-Control", - `max-age=${CONSTANTS.ERROR_CACHE_SECONDS / 2}, s-maxage=${ - CONSTANTS.ERROR_CACHE_SECONDS - }, stale-while-revalidate=${CONSTANTS.ONE_DAY}`, - ); // Use lower cache period for errors. - return res.send( - renderError(err.message, err.secondaryMessage, { - title_color, - text_color, - bg_color, - border_color, - theme, - }), + setCacheControlHeaders(res, CONSTANTS.ERROR_CACHE_SECONDS); + return renderResponse( + res, + renderError(err.message, err.secondaryMessage, options), + 500 ); } };