Skip to content

Commit

Permalink
[Issue #1823]: Fix prod FE error (#1824)
Browse files Browse the repository at this point in the history
## Summary
Fixes #1823 

## Changes proposed
- Add error handling when we have invalid JSON returned (when API is up
but not endpoints are enabled)
  • Loading branch information
rylew1 authored Apr 25, 2024
1 parent 960cdd6 commit 4a6a855
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
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
62 changes: 50 additions & 12 deletions frontend/src/app/search/error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,29 @@ 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;
const pagination_info = getErrorPaginationInfo();
let convertedSearchParams;
if (!isValidJSON(error.message)) {
// the error likely is just a string with a non-specific Server Component error when running the built app
// "An error occurred in the Server Components render. The specific message is omitted in production builds..."
parsedErrorData = getParsedError();
convertedSearchParams = parsedErrorData.searchInputs;
} else {
// Valid error thrown from server component
parsedErrorData = JSON.parse(error.message) as ParsedError;

// The error message search inputs had to be converted to arrays in order to be stringified,
// convert those back to sets as we do in non-error flow.
convertedSearchParams = convertSearchInputArraysToSets(
parsedErrorData.searchInputs,
);
}

const initialSearchResults: SearchAPIResponse = getErrorInitialSearchResults(
parsedErrorData,
pagination_info,
);

// The error message search inputs had to be converted to arrays in order to be stringified,
// convert those back to sets as we do in non-error flow.
const convertedSearchParams = convertSearchInputArraysToSets(
parsedErrorData.searchInputs,
parsedErrorData,
);

useEffect(() => {
Expand All @@ -65,15 +76,15 @@ export default function Error({ error }: ErrorProps) {
* which otherwise may not have any data.
*/
function getErrorInitialSearchResults(
parsedError: ParsedError,
pagination_info: PaginationInfo,
parsedError: ParsedError,
) {
return {
errors: [{ ...parsedError }],
errors: parsedError ? [{ ...parsedError }] : [{}],
data: [],
pagination_info,
status_code: parsedError.status,
message: parsedError.message,
status_code: parsedError?.status || -1,
message: parsedError?.message || "Unable to parse thrown error",
};
}

Expand Down Expand Up @@ -103,3 +114,30 @@ function convertSearchInputArraysToSets(
category: new Set(searchInputs.category || []),
};
}

function isValidJSON(str: string) {
try {
JSON.parse(str);
return true;
} catch (e) {
return false; // String is not valid JSON
}
}

function getParsedError() {
return {
type: "NetworkError",
searchInputs: {
status: new Set(),
fundingInstrument: new Set(),
eligibility: new Set(),
agency: new Set(),
category: new Set(),
sortby: null,
page: 1,
actionType: "initialLoad",
},
message: "Invalid JSON returned",
status: -1,
} as ParsedError;
}
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 4a6a855

Please sign in to comment.