Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions api/gist.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
setErrorCacheHeaders,
} from "../src/common/cache.js";
import { guardAccess } from "../src/common/access.js";
import { retrieveSecondaryMessage } from "../src/common/error.js";

// @ts-ignore
export default async (req, res) => {
const {
id,
Expand Down Expand Up @@ -89,10 +91,24 @@
);
} catch (err) {
setErrorCacheHeaders(res);
if (err instanceof Error) {
return res.send(
renderError({
message: err.message,
secondaryMessage: retrieveSecondaryMessage(err),
renderOptions: {
title_color,
text_color,
bg_color,
border_color,
theme,
},
}),
);
}
return res.send(
renderError({
message: err.message,
secondaryMessage: err.secondaryMessage,
message: "An unknown error occurred",
renderOptions: {
title_color,
text_color,
Expand Down
20 changes: 18 additions & 2 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
setCacheHeaders,
setErrorCacheHeaders,
} from "../src/common/cache.js";
import { retrieveSecondaryMessage } from "../src/common/error.js";
import { parseArray, parseBoolean, renderError } from "../src/common/utils.js";
import { fetchStats } from "../src/fetchers/stats.js";
import { isLocaleAvailable } from "../src/translations.js";

// @ts-ignore
export default async (req, res) => {
const {
username,
Expand Down Expand Up @@ -127,10 +129,24 @@
);
} catch (err) {
setErrorCacheHeaders(res);
if (err instanceof Error) {
return res.send(
renderError({
message: err.message,
secondaryMessage: retrieveSecondaryMessage(err),
renderOptions: {
title_color,
text_color,
bg_color,
border_color,
theme,
},
}),
);
}
return res.send(
renderError({
message: err.message,
secondaryMessage: err.secondaryMessage,
message: "An unknown error occurred",
renderOptions: {
title_color,
text_color,
Expand Down
20 changes: 18 additions & 2 deletions api/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
setCacheHeaders,
setErrorCacheHeaders,
} from "../src/common/cache.js";
import { retrieveSecondaryMessage } from "../src/common/error.js";
import { parseBoolean, renderError } from "../src/common/utils.js";
import { fetchRepo } from "../src/fetchers/repo.js";
import { isLocaleAvailable } from "../src/translations.js";

// @ts-ignore
export default async (req, res) => {
const {
username,
Expand Down Expand Up @@ -92,10 +94,24 @@
);
} catch (err) {
setErrorCacheHeaders(res);
if (err instanceof Error) {
return res.send(
renderError({
message: err.message,
secondaryMessage: retrieveSecondaryMessage(err),
renderOptions: {
title_color,
text_color,
bg_color,
border_color,
theme,
},
}),
);
}
return res.send(
renderError({
message: err.message,
secondaryMessage: err.secondaryMessage,
message: "An unknown error occurred",
renderOptions: {
title_color,
text_color,
Expand Down
20 changes: 18 additions & 2 deletions api/top-langs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
setCacheHeaders,
setErrorCacheHeaders,
} from "../src/common/cache.js";
import { retrieveSecondaryMessage } from "../src/common/error.js";
import { parseArray, parseBoolean, renderError } from "../src/common/utils.js";
import { fetchTopLanguages } from "../src/fetchers/top-languages.js";
import { isLocaleAvailable } from "../src/translations.js";

// @ts-ignore
export default async (req, res) => {
const {
username,
Expand Down Expand Up @@ -150,10 +152,24 @@
);
} catch (err) {
setErrorCacheHeaders(res);
if (err instanceof Error) {
return res.send(
renderError({
message: err.message,
secondaryMessage: retrieveSecondaryMessage(err),
renderOptions: {
title_color,
text_color,
bg_color,
border_color,
theme,
},
}),
);
}
return res.send(
renderError({
message: err.message,
secondaryMessage: err.secondaryMessage,
message: "An unknown error occurred",
renderOptions: {
title_color,
text_color,
Expand Down
20 changes: 18 additions & 2 deletions api/wakatime.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
setErrorCacheHeaders,
} from "../src/common/cache.js";
import { guardAccess } from "../src/common/access.js";
import { retrieveSecondaryMessage } from "../src/common/error.js";

// @ts-ignore
export default async (req, res) => {
const {
username,
Expand Down Expand Up @@ -108,10 +110,24 @@
);
} catch (err) {
setErrorCacheHeaders(res);
if (err instanceof Error) {
return res.send(
renderError({
message: err.message,
secondaryMessage: retrieveSecondaryMessage(err),
renderOptions: {
title_color,
text_color,
bg_color,
border_color,
theme,
},
}),
);
}
return res.send(
renderError({
message: err.message,
secondaryMessage: err.secondaryMessage,
message: "An unknown error occurred",
renderOptions: {
title_color,
text_color,
Expand Down
15 changes: 15 additions & 0 deletions src/common/error.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

/**
* @type {string} A general message to ask user to try again later.
*/
Expand Down Expand Up @@ -61,9 +63,22 @@ class MissingParamError extends Error {
}
}

/**
* Retrieve secondary message from an error object.
*
* @param {Error} err The error object.
* @returns {string|undefined} The secondary message if available, otherwise undefined.
*/
const retrieveSecondaryMessage = (err) => {
return "secondaryMessage" in err && typeof err.secondaryMessage === "string"
? err.secondaryMessage
: undefined;
};

export {
CustomError,
MissingParamError,
SECONDARY_ERROR_MESSAGES,
TRY_AGAIN_LATER,
retrieveSecondaryMessage,
};
Loading