Unverified Commit ee513240 authored by Anurag Hazra's avatar Anurag Hazra Committed by GitHub
Browse files

feat: adde CustomError class for better secondary errors (#355)

parent 4df291e6
Loading
Loading
Loading
Loading
+1 −6
Original line number Diff line number Diff line
@@ -38,12 +38,7 @@ module.exports = async (req, res) => {
      parseBoolean(include_all_commits)
    );
  } catch (err) {
    return res.send(
      renderError(
        err.message,
        "Make sure the provided username is not an organization"
      )
    );
    return res.send(renderError(err.message, err.secondaryMessage));
  }

  const cacheSeconds = clampValue(
+1 −3
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ const {
  parseBoolean,
  clampValue,
  CONSTANTS,
  logger,
} = require("../src/common/utils");
const fetchRepo = require("../src/fetchers/repo-fetcher");
const renderRepoCard = require("../src/cards/repo-card");
@@ -29,8 +28,7 @@ module.exports = async (req, res) => {
  try {
    repoData = await fetchRepo(username, repo);
  } catch (err) {
    logger.error(err);
    return res.send(renderError(err.message));
    return res.send(renderError(err.message, err.secondaryMessage));
  }

  let cacheSeconds = clampValue(
+1 −1
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ module.exports = async (req, res) => {
  try {
    topLangs = await fetchTopLanguages(username);
  } catch (err) {
    return res.send(renderError(err.message));
    return res.send(renderError(err.message, err.secondaryMessage));
  }

  const cacheSeconds = clampValue(
+2 −2
Original line number Diff line number Diff line
const { logger } = require("../common/utils");
const { logger, CustomError } = require("../common/utils");

const retryer = async (fetcher, variables, retries = 0) => {
  if (retries > 7) {
    throw new Error("Maximum retries exceeded");
    throw new CustomError("Maximum retries exceeded", CustomError.MAX_RETRY);
  }
  try {
    logger.log(`Trying PAT_${retries + 1}`);
+19 −1
Original line number Diff line number Diff line
@@ -170,6 +170,23 @@ const CONSTANTS = {
  ONE_DAY: 86400,
};

const SECONDARY_ERROR_MESSAGES = {
  MAX_RETRY:
    "Please add an env variable called PAT_1 with your github token in vercel",
  USER_NOT_FOUND: "Make sure the provided username is not an organization",
};

class CustomError extends Error {
  constructor(message, type) {
    super(message);
    this.type = type;
    this.secondaryMessage = SECONDARY_ERROR_MESSAGES[type] || "adsad";
  }

  static MAX_RETRY = "MAX_RETRY";
  static USER_NOT_FOUND = "USER_NOT_FOUND";
}

module.exports = {
  renderError,
  kFormatter,
@@ -185,4 +202,5 @@ module.exports = {
  wrapTextMultiline,
  logger,
  CONSTANTS,
  CustomError,
};
Loading