From 3ac9e7da14b521390aa1c1c79b880eb9a71aa7e9 Mon Sep 17 00:00:00 2001 From: Jordan Yee Date: Wed, 28 Feb 2024 16:33:06 -0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20changed=20error=20response?= =?UTF-8?q?=20to=20success=20flag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit re #115 --- apps/api/src/routes/v1/graphql/lib.ts | 3 +-- libs/lambda/src/response.ts | 8 ++++---- packages/types/types/response.ts | 3 ++- packages/websoc-fuzzy-search/setup.ts | 5 ++--- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/apps/api/src/routes/v1/graphql/lib.ts b/apps/api/src/routes/v1/graphql/lib.ts index a643400a..a73f6e2a 100644 --- a/apps/api/src/routes/v1/graphql/lib.ts +++ b/apps/api/src/routes/v1/graphql/lib.ts @@ -1,6 +1,5 @@ import type { BaseContext, HTTPGraphQLResponse } from "@apollo/server"; import type { IFieldResolver } from "@graphql-tools/utils"; -import { isErrorResponse } from "@peterportal-api/types"; import type { RawResponse } from "@peterportal-api/types"; import { GraphQLError } from "graphql/error"; @@ -58,7 +57,7 @@ export const proxyRestApi = }; }); - if (isErrorResponse(data)) { + if (!data.success) { throw new GraphQLError(data.message, { extensions: { code: data.error.toUpperCase().replace(" ", "_"), diff --git a/libs/lambda/src/response.ts b/libs/lambda/src/response.ts index a4e90d62..3038f91d 100644 --- a/libs/lambda/src/response.ts +++ b/libs/lambda/src/response.ts @@ -47,7 +47,7 @@ export function createOKResult( ): APIGatewayProxyResult { const statusCode = 200; const timestamp = createTimestamp(); - const response: Response = { statusCode, timestamp, requestId, payload }; + const response: Response = { statusCode, timestamp, requestId, payload, success: true }; const headers = { ...responseHeaders }; try { @@ -89,12 +89,12 @@ export function createErrorResult( e instanceof Error ? `${e.name}: ${e.message}` : typeof e === "string" - ? e - : "An unknown error has occurred. Please try again."; + ? e + : "An unknown error has occurred. Please try again."; const error = httpErrorCodes[statusCode as keyof typeof httpErrorCodes]; - const body: ErrorResponse = { timestamp, requestId, statusCode, error, message }; + const body: ErrorResponse = { timestamp, requestId, statusCode, error, message, success: false }; logger.error(`${body.statusCode} ${body.error}: ${body.message}`); diff --git a/packages/types/types/response.ts b/packages/types/types/response.ts index 21814103..31098fd2 100644 --- a/packages/types/types/response.ts +++ b/packages/types/types/response.ts @@ -14,6 +14,7 @@ export type BaseResponse = { * The status code of the request. */ statusCode: number; + success: boolean; }; /** @@ -54,4 +55,4 @@ export type RawResponse = Response | ErrorResponse; * ``ErrorResponse`` or a ``Response``. * @param r The object to test. */ -export const isErrorResponse = (r: RawResponse): r is ErrorResponse => "error" in r; +export const isErrorResponse = (r: RawResponse): r is ErrorResponse => r.success == false; diff --git a/packages/websoc-fuzzy-search/setup.ts b/packages/websoc-fuzzy-search/setup.ts index 358782dc..fb007476 100644 --- a/packages/websoc-fuzzy-search/setup.ts +++ b/packages/websoc-fuzzy-search/setup.ts @@ -2,7 +2,6 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { normalize } from "node:path"; import { gzipSync } from "node:zlib"; -import { isErrorResponse } from "@peterportal-api/types"; import type { Course, Instructor, RawResponse } from "@peterportal-api/types"; import fetch from "cross-fetch"; import pluralize from "pluralize"; @@ -219,7 +218,7 @@ async function main() { console.time("Data fetched in"); const coursesRes = await fetch("https://api-next.peterportal.org/v1/rest/courses/all"); const coursesJson: RawResponse = await coursesRes.json(); - if (isErrorResponse(coursesJson)) throw new Error("Could not fetch courses from API."); + if (!coursesJson.success) throw new Error("Could not fetch courses from API."); coursesJson.payload.forEach( ({ id, department, departmentName, courseNumber, geList, courseLevel, school, title }) => { d.courses[id] = { @@ -235,7 +234,7 @@ async function main() { ); const instructorsRes = await fetch("https://api-next.peterportal.org/v1/rest/instructors/all"); const instructorsJson: RawResponse = await instructorsRes.json(); - if (isErrorResponse(instructorsJson)) throw new Error("Could not fetch instructors from API."); + if (!coursesJson.success) throw new Error("Could not fetch instructors from API."); instructorsJson.payload.forEach(({ ucinetid, shortenedName, name, schools, department }) => { d.instructors[ucinetid] = { ucinetid,