Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix types on error message extraction #1689

Merged
merged 1 commit into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions frontend/apps/remark42/app/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,13 @@ export interface Image {

/** error struct returned in case of api call error */
export interface ApiError {
code: number;
/**
* Error code, that is part of server error response.
* Note that -1 is reserved for error where `error` field shall be used directly
*/
code?: number;
/** simple explanation */
details: string;
details?: string;
/** in-depth explanation */
error: string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,8 @@
default:
break;
}
} catch (e) {
// @ts-ignore
setError(extractErrorMessageFromResponse(e, intl));
} catch (err) {
setError(extractErrorMessageFromResponse(err, intl));

Check warning on line 170 in frontend/apps/remark42/app/components/comment-form/__subscribe-by-email/comment-form__subscribe-by-email.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/apps/remark42/app/components/comment-form/__subscribe-by-email/comment-form__subscribe-by-email.tsx#L170

Added line #L170 was not covered by tests
} finally {
setLoading(false);
}
Expand Down Expand Up @@ -226,9 +225,8 @@
await unsubscribeFromEmailUpdates();
dispatch(setUserSubscribed(false));
setStep(Step.Unsubscribed);
} catch (e) {
// @ts-ignore
setError(extractErrorMessageFromResponse(e, intl));
} catch (err) {
setError(extractErrorMessageFromResponse(err, intl));

Check warning on line 229 in frontend/apps/remark42/app/components/comment-form/__subscribe-by-email/comment-form__subscribe-by-email.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/apps/remark42/app/components/comment-form/__subscribe-by-email/comment-form__subscribe-by-email.tsx#L229

Added line #L229 was not covered by tests
} finally {
setLoading(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@
return;
}
setStep('subscribed');
} catch (e) {
setError(extractErrorMessageFromResponse(e as FetcherError, intl));
} catch (err) {
setError(extractErrorMessageFromResponse(err, intl));

Check warning on line 101 in frontend/apps/remark42/app/components/comment-form/__subscribe-by-telegram/comment-form__subscribe-by-telegram.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/apps/remark42/app/components/comment-form/__subscribe-by-telegram/comment-form__subscribe-by-telegram.tsx#L101

Added line #L101 was not covered by tests
} finally {
setLoading(false);
}
Expand All @@ -119,8 +119,8 @@
}, 0);
await telegramUnsubcribe();
setStep('unsubscribed');
} catch (e) {
setError(extractErrorMessageFromResponse(e as FetcherError, intl));
} catch (err) {
setError(extractErrorMessageFromResponse(err, intl));

Check warning on line 123 in frontend/apps/remark42/app/components/comment-form/__subscribe-by-telegram/comment-form__subscribe-by-telegram.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/apps/remark42/app/components/comment-form/__subscribe-by-telegram/comment-form__subscribe-by-telegram.tsx#L123

Added line #L123 was not covered by tests
} finally {
setLoading(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { FormattedMessage, IntlShape, defineMessages } from 'react-intl';
import b, { Mix } from 'bem-react-helper';

import { User, Theme, Image, ApiError } from 'common/types';

Check warning on line 5 in frontend/apps/remark42/app/components/comment-form/comment-form.tsx

View workflow job for this annotation

GitHub Actions / Eslint & Stylelint (16.15.1)

'ApiError' is defined but never used
import { StaticStore } from 'common/static-store';
import * as settings from 'common/settings';
import { extractErrorMessageFromResponse } from 'utils/errorUtils';
Expand Down Expand Up @@ -156,12 +156,11 @@
this.setState({ isDisabled: true, isErrorShown: false, text });
try {
await this.props.onSubmit(text, settings.pageTitle || document.title);
} catch (e) {
} catch (err) {
this.setState({
isDisabled: false,
isErrorShown: true,
// @ts-ignore
errorMessage: extractErrorMessageFromResponse(e, this.props.intl),
errorMessage: extractErrorMessageFromResponse(err, this.props.intl),
});
return;
}
Expand All @@ -180,8 +179,8 @@
this.props
.getPreview(text)
.then((preview) => this.setState({ preview }))
.catch((e) => {
this.setState({ isErrorShown: true, errorMessage: extractErrorMessageFromResponse(e, this.props.intl) });
.catch((err) => {
this.setState({ isErrorShown: true, errorMessage: extractErrorMessageFromResponse(err, this.props.intl) });

Check warning on line 183 in frontend/apps/remark42/app/components/comment-form/comment-form.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/apps/remark42/app/components/comment-form/comment-form.tsx#L182-L183

Added lines #L182 - L183 were not covered by tests
});
};

Expand Down Expand Up @@ -260,13 +259,13 @@
}

/** wrapper with error handling for props.uploadImage */
uploadImage = (file: File): Promise<Image | Error> => {
uploadImage = async (file: File): Promise<Image | Error> => {
const intl = this.props.intl;
return this.props.uploadImage!(file).catch((e: ApiError | string) => {
return this.props.uploadImage!(file).catch((err) => {

Check warning on line 264 in frontend/apps/remark42/app/components/comment-form/comment-form.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/apps/remark42/app/components/comment-form/comment-form.tsx#L264

Added line #L264 was not covered by tests
return new Error(
intl.formatMessage(messages.uploadFileFail, {
fileName: file.name,
errorMessage: extractErrorMessageFromResponse(e, this.props.intl),
errorMessage: extractErrorMessageFromResponse(err, this.props.intl),
})
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export function CommentVotes({ id, votes, vote, disabled }: Props) {
setErrorMessage(undefined);
setTimeout(() => setLoadingState(null), 200);
} catch (err) {
// @ts-ignore
setErrorMessage(extractErrorMessageFromResponse(err, intl));
setLoadingState(null);
}
Expand Down
16 changes: 3 additions & 13 deletions frontend/apps/remark42/app/utils/errorUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IntlShape, defineMessages } from 'react-intl';
import { ApiError } from '../common/types';

export const errorMessages = defineMessages<string | number>({
'fetch-error': {
Expand Down Expand Up @@ -111,25 +112,14 @@ export const errorMessages = defineMessages<string | number>({
},
});

export type FetcherError =
| string
| {
/**
* Error code, that is part of server error response.
*
* Note that -1 is reserved for error where `error` field shall be used directly
*/
code?: number;
details?: string;
error: string;
};
export type FetcherError = string | ApiError | RequestError | unknown;

export function extractErrorMessageFromResponse(response: FetcherError, intl: IntlShape): string {
if (typeof response === 'string') {
return response;
}

if (typeof response.code === 'number' && errorMessages[response.code]) {
if (response instanceof RequestError) {
return intl.formatMessage(errorMessages[response.code]);
}

Expand Down
Loading