mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-08-11 06:20:22 +02:00
feat: improve error message
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
import * as express from "express";
|
||||
import AnonymizedFile from "../AnonymizedFile";
|
||||
import AnonymousError from "../AnonymousError";
|
||||
import * as db from "../database/database";
|
||||
import UserModel from "../database/users/users.model";
|
||||
import Repository from "../Repository";
|
||||
import GitHubBase from "../source/GitHubBase";
|
||||
import GitHubDownload from "../source/GitHubDownload";
|
||||
import { GitHubRepository } from "../source/GitHubRepository";
|
||||
import User from "../User";
|
||||
|
||||
export async function getRepo(
|
||||
@@ -31,8 +37,35 @@ export async function getRepo(
|
||||
}
|
||||
}
|
||||
|
||||
function printError(error: any) {
|
||||
if (error instanceof AnonymousError) {
|
||||
let detail = error.value?.toString();
|
||||
if (error.value instanceof Repository) {
|
||||
detail = error.value.repoId;
|
||||
} else if (error.value instanceof AnonymizedFile) {
|
||||
detail = `/r/${error.value.repository.repoId}/${error.value.anonymizedPath}`;
|
||||
} else if (error.value instanceof GitHubRepository) {
|
||||
detail = `${error.value.fullName}`;
|
||||
} else if (error.value instanceof User) {
|
||||
detail = `${error.value.username}`;
|
||||
} else if (error.value instanceof GitHubBase) {
|
||||
detail = `${error.value.repository.repoId}`;
|
||||
}
|
||||
console.error(
|
||||
"[ERROR]",
|
||||
error.message,
|
||||
`'${detail}'`,
|
||||
error.stack.split("\n")[1].trim()
|
||||
);
|
||||
} else if (error instanceof Error) {
|
||||
console.error(error);
|
||||
} else {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
export function handleError(error: any, res: express.Response) {
|
||||
console.log(error);
|
||||
printError(error);
|
||||
let message = error;
|
||||
if (error instanceof Error) {
|
||||
message = error.message;
|
||||
@@ -52,12 +85,12 @@ export async function getUser(req: express.Request) {
|
||||
const user = (req.user as any).user;
|
||||
if (!user) {
|
||||
req.logout();
|
||||
throw new Error("not_connected");
|
||||
throw new AnonymousError("not_connected");
|
||||
}
|
||||
const model = await UserModel.findById(user._id);
|
||||
if (!model) {
|
||||
req.logout();
|
||||
throw new Error("not_connected");
|
||||
throw new AnonymousError("not_connected");
|
||||
}
|
||||
return new User(model);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user