Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

feat: ✨ changed error response to success flag #134

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions apps/api/src/routes/v1/graphql/lib.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -58,13 +57,13 @@
};
});

if (isErrorResponse(data)) {
if (!data.success) {
throw new GraphQLError(data.message, {

Check failure on line 61 in apps/api/src/routes/v1/graphql/lib.ts

View workflow job for this annotation

GitHub Actions / Check for TypeScript errors

Property 'message' does not exist on type 'RawResponse<unknown>'.
extensions: {
code: data.error.toUpperCase().replace(" ", "_"),

Check failure on line 63 in apps/api/src/routes/v1/graphql/lib.ts

View workflow job for this annotation

GitHub Actions / Check for TypeScript errors

Property 'error' does not exist on type 'RawResponse<unknown>'.
},
});
}

return data.payload;

Check failure on line 68 in apps/api/src/routes/v1/graphql/lib.ts

View workflow job for this annotation

GitHub Actions / Check for TypeScript errors

Property 'payload' does not exist on type 'RawResponse<unknown>'.
};
8 changes: 4 additions & 4 deletions libs/lambda/src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function createOKResult<T>(
): APIGatewayProxyResult {
const statusCode = 200;
const timestamp = createTimestamp();
const response: Response<T> = { statusCode, timestamp, requestId, payload };
const response: Response<T> = { statusCode, timestamp, requestId, payload, success: true };
const headers = { ...responseHeaders };

try {
Expand Down Expand Up @@ -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}`);

Expand Down
3 changes: 2 additions & 1 deletion packages/types/types/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type BaseResponse = {
* The status code of the request.
*/
statusCode: number;
success: boolean;
};

/**
Expand Down Expand Up @@ -54,4 +55,4 @@ export type RawResponse<T> = Response<T> | ErrorResponse;
* ``ErrorResponse`` or a ``Response<T>``.
* @param r The object to test.
*/
export const isErrorResponse = <T>(r: RawResponse<T>): r is ErrorResponse => "error" in r;
export const isErrorResponse = <T>(r: RawResponse<T>): r is ErrorResponse => r.success == false;
5 changes: 2 additions & 3 deletions packages/websoc-fuzzy-search/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
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";
Expand Down Expand Up @@ -219,9 +218,9 @@
console.time("Data fetched in");
const coursesRes = await fetch("https://api-next.peterportal.org/v1/rest/courses/all");
const coursesJson: RawResponse<Course[]> = 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(

Check failure on line 222 in packages/websoc-fuzzy-search/setup.ts

View workflow job for this annotation

GitHub Actions / Check for TypeScript errors

Property 'payload' does not exist on type 'RawResponse<Course[]>'.
({ id, department, departmentName, courseNumber, geList, courseLevel, school, title }) => {

Check failure on line 223 in packages/websoc-fuzzy-search/setup.ts

View workflow job for this annotation

GitHub Actions / Check for TypeScript errors

Binding element 'id' implicitly has an 'any' type.

Check failure on line 223 in packages/websoc-fuzzy-search/setup.ts

View workflow job for this annotation

GitHub Actions / Check for TypeScript errors

Binding element 'department' implicitly has an 'any' type.

Check failure on line 223 in packages/websoc-fuzzy-search/setup.ts

View workflow job for this annotation

GitHub Actions / Check for TypeScript errors

Binding element 'departmentName' implicitly has an 'any' type.

Check failure on line 223 in packages/websoc-fuzzy-search/setup.ts

View workflow job for this annotation

GitHub Actions / Check for TypeScript errors

Binding element 'courseNumber' implicitly has an 'any' type.

Check failure on line 223 in packages/websoc-fuzzy-search/setup.ts

View workflow job for this annotation

GitHub Actions / Check for TypeScript errors

Binding element 'geList' implicitly has an 'any' type.

Check failure on line 223 in packages/websoc-fuzzy-search/setup.ts

View workflow job for this annotation

GitHub Actions / Check for TypeScript errors

Binding element 'courseLevel' implicitly has an 'any' type.
d.courses[id] = {
department,
department_name: departmentName,
Expand All @@ -235,7 +234,7 @@
);
const instructorsRes = await fetch("https://api-next.peterportal.org/v1/rest/instructors/all");
const instructorsJson: RawResponse<Instructor[]> = 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,
Expand Down
Loading