Skip to content

Commit

Permalink
Merge pull request #1885 from DISSINET/1879-add-new-connection-to-ser…
Browse files Browse the repository at this point in the history
…ver-lost-error-toast

1879 add new connection to server lost error toast
  • Loading branch information
jancimertel authored Nov 3, 2023
2 parents 1d728df + d9812b8 commit f4d422b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 4 additions & 2 deletions packages/client/src/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ class Api {
message: "",
};

if (responseData && typeof responseData === "object") {
if (responseData instanceof AxiosError && (responseData as AxiosError).code === AxiosError.ERR_NETWORK) {
out.error = errors.NetworkError.name
} else if (responseData && (responseData as any).response && (responseData as any).response.data) {
out.error = (responseData as Record<string, string>).error;
out.message = (responseData as Record<string, string>).message;
}
Expand All @@ -193,7 +195,7 @@ class Api {

showErrorToast(err: any) {
const hydratedError = errors.getErrorByCode(
this.responseToError(err.response?.data)
this.responseToError(err)
);

toast.error(
Expand Down
10 changes: 9 additions & 1 deletion packages/shared/types/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@ class UnknownError extends CustomError {
public static message = "Mysterious";
}

class NetworkError extends CustomError {
public static code = 500;
public static title = "Connection to server lost";
public static message = "Please check your network connection. Otherwise contact the administrator.";
}

const allErrors: Record<string, any> = {
InvalidDeleteError,
UnauthorizedError,
Expand All @@ -359,6 +365,7 @@ const allErrors: Record<string, any> = {
EmailError,
RelationDoesNotExist,
DocumentDoesNotExist,
NetworkError,
};

export interface IErrorSignature {
Expand All @@ -369,7 +376,7 @@ export interface IErrorSignature {
export function getErrorByCode(errSig: IErrorSignature): CustomError {
return allErrors[errSig.error]
? new allErrors[errSig.error](errSig.message)
: new UnknownError(errSig.message || "Something bad happened");
: new UnknownError(errSig.message || "Unknown error occured")
}

export {
Expand All @@ -396,4 +403,5 @@ export {
EmailError,
RelationDoesNotExist,
DocumentDoesNotExist,
NetworkError
};

0 comments on commit f4d422b

Please sign in to comment.