From 675bc15a97c0633a585a768f38fe6cfe499e1156 Mon Sep 17 00:00:00 2001 From: DongZoo Lee Date: Thu, 12 Jan 2023 18:47:40 +0900 Subject: [PATCH] Remove client handle on Codeforces API request fails --- utils/cofo.js | 9 +++++++-- utils/sql.js | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/utils/cofo.js b/utils/cofo.js index 4b6e683..2b497f0 100644 --- a/utils/cofo.js +++ b/utils/cofo.js @@ -1,6 +1,6 @@ const cofo = (module.exports = {}); const axios = require("axios"); - +const sql = require("./sql"); const logger = require("./logger"); cofo.getTier = async (id) => { @@ -17,7 +17,12 @@ cofo.getTier = async (id) => { return await cofo.getTier(changedHandle); } - logger.exception(error.response.data.comment); + const reg = new RegExp("handles: User with handle (.*?) not found"); + const errorMessage = error.response.data.comment; + const matched = errorMessage.match(reg); + if (matched && matched[1]) sql.removeClient(matched[1]); + + logger.exception(`${error.response.data.comment} and was removed.`); return { status: "FAILED", result: [] }; } diff --git a/utils/sql.js b/utils/sql.js index 357c0f2..10ad875 100644 --- a/utils/sql.js +++ b/utils/sql.js @@ -50,3 +50,10 @@ sql.getClients = async () => { console.error(error); } }; +sql.removeClient = async (handle) => { + try { + await connection.query(`DELETE FROM clients WHERE handle=?`, [handle]); + } catch (error) { + console.error(error); + } +};