Skip to content

Commit

Permalink
Refactor error handling to not be global
Browse files Browse the repository at this point in the history
  • Loading branch information
RachelElysia committed Mar 22, 2024
1 parent 5380129 commit 53a7ab1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ interface ICalendarsFormData {
apiKeyJson?: string;
}

// Used to surface error.message in UI of unknown error type
type ErrorWithMessage = {
message: string;
[key: string]: unknown;
};

const isErrorWithMessage = (error: unknown): error is ErrorWithMessage => {
return (error as ErrorWithMessage).message !== undefined;
};

const baseClass = "calendars-integration";

const Calendars = (): JSX.Element => {
Expand Down Expand Up @@ -108,8 +118,12 @@ const Calendars = (): JSX.Element => {
if (curFormData.apiKeyJson) {
try {
JSON.parse(curFormData.apiKeyJson);
} catch (e) {
errors.apiKeyJson = e.message.toString();
} catch (e: unknown) {
if (isErrorWithMessage(e)) {
errors.apiKeyJson = e.message.toString();
} else {
throw e;
}
}
}
return errors;
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"jsx": "react",
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"lib": ["ES2021.String"],
"useUnknownInCatchVariables": false
"lib": ["ES2021.String"]
},
"include": ["./frontend/**/*"],
"exclude": ["node_modules"],
Expand Down

0 comments on commit 53a7ab1

Please sign in to comment.