From 1dd97d42a15b540e8064ab0dd2d7c77c7cf66d21 Mon Sep 17 00:00:00 2001 From: Vyrtsev Mikhail Date: Mon, 15 Apr 2019 07:54:51 +0300 Subject: [PATCH] hide unexpected error message from ui, still can be seen in console --- web/app/common/fetcher.ts | 4 +++- web/app/components/comment/comment.tsx | 4 ++-- web/app/utils/errorUtils.ts | 5 +++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/web/app/common/fetcher.ts b/web/app/common/fetcher.ts index a72c4c0d..908bdd04 100644 --- a/web/app/common/fetcher.ts +++ b/web/app/common/fetcher.ts @@ -81,7 +81,9 @@ const fetcher = methods.reduce>((acc, method) => { try { err = JSON.parse(text); } catch (e) { - throw text; + // eslint-disable-next-line no-console + console.error(err); + throw 'Something went wrong.'; } throw err; }); diff --git a/web/app/components/comment/comment.tsx b/web/app/components/comment/comment.tsx index 575d0db8..d27e80af 100644 --- a/web/app/components/comment/comment.tsx +++ b/web/app/components/comment/comment.tsx @@ -12,7 +12,7 @@ import { StaticStore } from '@app/common/static_store'; import debounce from '@app/utils/debounce'; import copy from '@app/common/copy'; import { Theme, BlockTTL, Comment as CommentType, PostInfo, User, CommentMode, Image } from '@app/common/types'; -import { extractErrorMessageFromResponse, FetcherResponse } from '@app/utils/errorUtils'; +import { extractErrorMessageFromResponse, FetcherError } from '@app/utils/errorUtils'; import { Input } from '@app/components/input'; import { AvatarIcon } from '@app/components/avatar-icon'; @@ -213,7 +213,7 @@ export class Comment extends Component { } } - handleVoteError(e: FetcherResponse, originalScore: number, originalDelta: number) { + handleVoteError(e: FetcherError, originalScore: number, originalDelta: number) { this.setState({ scoreDelta: originalDelta, cachedScore: originalScore, diff --git a/web/app/utils/errorUtils.ts b/web/app/utils/errorUtils.ts index 400612fe..4b85e081 100644 --- a/web/app/utils/errorUtils.ts +++ b/web/app/utils/errorUtils.ts @@ -20,14 +20,15 @@ const errorMessageForCodes = new Map([ [18, 'Requested file cannot be found.'], ]); -export type FetcherResponse = +export type FetcherError = | string | { code?: number; details?: string; + error: string; }; -export function extractErrorMessageFromResponse(response: FetcherResponse): string { +export function extractErrorMessageFromResponse(response: FetcherError): string { const defaultErrorMessage = 'Something went wrong. Please try again a bit later.'; if (!response) { return defaultErrorMessage;