Skip to content

Commit

Permalink
Merge pull request #63 from WoWAnalyzer/private-log-handling
Browse files Browse the repository at this point in the history
handle report not found errors
  • Loading branch information
emallson authored Jul 29, 2024
2 parents da60157 + e78580f commit 9e05430
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/route/wcl/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as crypto from "node:crypto";
import { FastifyInstance, FastifyRequest } from "fastify";
import * as cache from "../../cache.ts";
import * as Sentry from "@sentry/node";
import { ApiError, ApiErrorType } from "../../wcl/api.ts";

export type WclProxy<T, P = ReportParams> = { Params: P; Querystring: T };
export type ReportParams = { code: string };
Expand Down Expand Up @@ -95,6 +96,14 @@ export function wrapEndpoint<
}
}
} catch (error) {
if (
error instanceof ApiError &&
error.type === ApiErrorType.NoSuchLog
) {
return reply.code(404).send({
message: "No log found with that code.",
});
}
console.error(error);
// TODO handle error
return reply.code(500).send({
Expand Down
1 change: 0 additions & 1 deletion src/route/wcl/fights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
ReportPlayer,
WCLFight,
WCLReport,
WithFights,
} from "./v1-types";

const fightQuery = gql`
Expand Down
50 changes: 47 additions & 3 deletions src/wcl/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { request, Variables } from "graphql-request";
import { ClientError, request, Variables } from "graphql-request";
import axios from "axios";

async function fetchToken(): Promise<string | undefined> {
Expand Down Expand Up @@ -31,6 +31,22 @@ async function getToken(force: boolean = false): Promise<string | undefined> {
return token;
}

export enum ApiErrorType {
/** The log is private or does not exist. */
NoSuchLog,
Unknown,
}

export class ApiError extends Error {
public readonly type: ApiErrorType;
public readonly cause: Error;
constructor(cause: Error, type: ApiErrorType) {
super(cause.message);
this.cause = cause;
this.type = type;
}
}

export async function query<T, V extends Variables>(
gql: string,
variables: V,
Expand All @@ -46,10 +62,38 @@ export async function query<T, V extends Variables>(
try {
data = await run();
} catch (error) {
// TODO: actually check status code
if (error instanceof ClientError) {
if (isPrivateLogError(error)) {
throw new ApiError(error, ApiErrorType.NoSuchLog);
}
}

// blindly attempt to reauthenticate and try again
token = await getToken(true);
data = await run();
try {
data = await run();
} catch (error) {
if (error instanceof ClientError) {
if (isPrivateLogError(error)) {
throw new ApiError(error, ApiErrorType.NoSuchLog);
}

// we only use Unknown here after attempting to re-auth to make sure that the re-auth happens
throw new ApiError(error, ApiErrorType.Unknown);
}

throw error;
}
}

return data;
}

function isPrivateLogError(error: ClientError): boolean {
return (
error.response.errors?.some(
(err) =>
err.message === "You do not have permission to view this report.",
) === true
);
}

0 comments on commit 9e05430

Please sign in to comment.