From 6f75185e744c4b3655fdaf377fdee4379049dc8e Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Mon, 22 Jul 2019 02:44:45 +0300 Subject: [PATCH] Handle OAuth2 token errors separately from regular server errors --- src/api/index.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/api/index.ts b/src/api/index.ts index c9b4b65c..113d8990 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,4 +1,4 @@ -import { error, notFound } from "./responses"; +import { clientError, error, notFound } from "./responses"; import express from "express"; import cors from "cors"; import { initAuth } from "./auth"; @@ -6,6 +6,7 @@ import { initGuildsAPI } from "./guilds"; import { initArchives } from "./archives"; import { connect } from "../data/db"; import path from "path"; +import { TokenError } from "passport-oauth2"; require("dotenv").config({ path: path.resolve(__dirname, "..", "..", "api.env") }); @@ -31,8 +32,12 @@ connect().then(() => { // Error response app.use((err, req, res, next) => { - console.error(err); - error(res, "Server error", err.status || 500); + if (err instanceof TokenError) { + clientError(res, "Invalid code"); + } else { + console.error(err); + error(res, "Server error", err.status || 500); + } }); // 404 response