Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rylew1 committed Apr 24, 2024
1 parent 597c2c1 commit 7ff2f3d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions frontend/src/app/api/BaseApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ const throwError = (
searchInputs: SearchFetcherProps,
firstError?: APIResponseError,
) => {
console.log("Throwing error: ", message, status_code, searchInputs);

// Include just firstError for now, we can expand this
// If we need ValidationErrors to be more expanded
const error = firstError ? { message, firstError } : { message };
Expand Down
34 changes: 33 additions & 1 deletion frontend/src/app/search/error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,30 @@ export interface ParsedError {
export default function Error({ error }: ErrorProps) {
// The error message is passed as an object that's been stringified.
// Parse it here.
const parsedErrorData = JSON.parse(error.message) as ParsedError;

let parsedErrorData;
if (!isValidJSON(error.message)) {
// if we have no endpoints enabled,
// the API may return invalid JSON
parsedErrorData = {
type: "NetworkError",
searchInputs: {
status: [],
query: "",
fundingInstrument: [],
eligibility: [],
agency: [],
category: [],
sortby: null,
page: 1,
actionType: "initialLoad",
},
message: "Invalid JSON returned",
status: -1,
};
}

parsedErrorData = JSON.parse(error.message) as ParsedError;

const pagination_info = getErrorPaginationInfo();
const initialSearchResults: SearchAPIResponse = getErrorInitialSearchResults(
Expand Down Expand Up @@ -103,3 +126,12 @@ function convertSearchInputArraysToSets(
category: new Set(searchInputs.category || []),
};
}

function isValidJSON(str: string) {
try {
JSON.parse(str);
return true; // String is valid JSON
} catch (e) {
return false; // String is not valid JSON
}
}
1 change: 1 addition & 0 deletions frontend/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class BaseFrontendError extends Error {
type: string,
status?: number,
) {
// Sets cannot be properly serialized so convert to arrays first
const serializedSearchInputs = convertSearchInputSetsToArrays(searchInputs);

const serializedData = JSON.stringify({
Expand Down

0 comments on commit 7ff2f3d

Please sign in to comment.