From 0334b9eb9838ada8ff946d07f93fa09a76f164e9 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Thu, 30 Apr 2020 22:54:16 +0800 Subject: [PATCH] feat: better error messages --- src/driver/core/SideBar.ts | 8 +++++-- src/platforms/GitHub/API.ts | 4 ++++ src/platforms/Gitee/API.ts | 48 ++++++++++++++++++++----------------- src/platforms/index.ts | 1 + 4 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/driver/core/SideBar.ts b/src/driver/core/SideBar.ts index 4e0bbb6..0c013c0 100644 --- a/src/driver/core/SideBar.ts +++ b/src/driver/core/SideBar.ts @@ -1,6 +1,6 @@ import { ConfigsContextShape } from 'containers/ConfigsContext' import { GetCreatedMethod, MethodCreator } from 'driver/connect' -import { errors, platform } from 'platforms' +import { errors, platform, platformName } from 'platforms' import * as DOMHelper from 'utils/DOMHelper' export type Props = { @@ -133,13 +133,17 @@ export const handleError: BoundMethodCreator<[Error]> = dispatch => async err => if (err.message === errors.EMPTY_PROJECT) { dispatch.call(setError, 'This project seems to be empty.') } else if (err.message === errors.BLOCKED_PROJECT) { - dispatch.call(setError, 'This project is blocked.') + dispatch.call(setError, 'Access to the project is blocked.') } else if ( err.message === errors.NOT_FOUND || err.message === errors.BAD_CREDENTIALS || err.message === errors.API_RATE_LIMIT ) { dispatch.set({ errorDueToAuth: true }) + } else if (err.message === errors.CONNECTION_BLOCKED) { + dispatch.call(setError, `Cannot connect to ${platformName}.`) + } else if (err.message === errors.SERVER_FAULT) { + dispatch.call(setError, `${platformName} server went down.`) } else { DOMHelper.markGitakoReadyState(false) dispatch.call(setError, 'Some thing went wrong.') diff --git a/src/platforms/GitHub/API.ts b/src/platforms/GitHub/API.ts index a7b47fe..a60de42 100644 --- a/src/platforms/GitHub/API.ts +++ b/src/platforms/GitHub/API.ts @@ -29,6 +29,7 @@ async function request( if (accessToken) { headers.Authorization = `token ${accessToken}` } + try { const res = await fetch(url, { headers }) const contentType = res.headers.get('Content-Type') || res.headers.get('content-type') const isJson = contentType?.includes('application/json') @@ -52,6 +53,9 @@ async function request( throw new Error(`Response content type is "${contentType}"`) } } + } catch (err) { + throw new Error(errors.CONNECTION_BLOCKED) + } } export async function getRepoMeta( diff --git a/src/platforms/Gitee/API.ts b/src/platforms/Gitee/API.ts index 9864c33..ac9d030 100644 --- a/src/platforms/Gitee/API.ts +++ b/src/platforms/Gitee/API.ts @@ -23,29 +23,33 @@ async function request( if (accessToken) { headers.Authorization = `token ${accessToken}` } - const res = await fetch(url, { headers }) - const contentType = res.headers.get('Content-Type') || res.headers.get('content-type') - if (!contentType) { - throw new Error(`Response has no content type`) - } else if (!contentType.includes('application/json')) { - throw new Error(`Response content type is ${contentType}`) - } - // About res.ok: - // True if res.status between 200~299 - // Ref: https://developer.mozilla.org/en-US/docs/Web/API/Response/ok - if (res.ok) { - return res.json() - } else { - if (res.status === 404 || res.status === 401) throw new Error(errors.NOT_FOUND) - else if (res.status === 500) throw new Error(errors.SERVER_FAULT) - else { - const content = await res.json() - if (isEmptyProject(content)) throw new Error(errors.EMPTY_PROJECT) - if (isBlockedProject(content)) throw new Error(errors.BLOCKED_PROJECT) - // Unknown type of error, report it! - raiseError(new Error(res.statusText)) - throw new Error(content && content.message) + try { + const res = await fetch(url, { headers }) + const contentType = res.headers.get('Content-Type') || res.headers.get('content-type') + if (!contentType) { + throw new Error(`Response has no content type`) + } else if (!contentType.includes('application/json')) { + throw new Error(`Response content type is ${contentType}`) } + // About res.ok: + // True if res.status between 200~299 + // Ref: https://developer.mozilla.org/en-US/docs/Web/API/Response/ok + if (res.ok) { + return res.json() + } else { + if (res.status === 404 || res.status === 401) throw new Error(errors.NOT_FOUND) + else if (res.status === 500) throw new Error(errors.SERVER_FAULT) + else { + const content = await res.json() + if (isEmptyProject(content)) throw new Error(errors.EMPTY_PROJECT) + if (isBlockedProject(content)) throw new Error(errors.BLOCKED_PROJECT) + // Unknown type of error, report it! + raiseError(new Error(res.statusText)) + throw new Error(content && content.message) + } + } + } catch (err) { + throw new Error(errors.CONNECTION_BLOCKED) } } diff --git a/src/platforms/index.ts b/src/platforms/index.ts index 41d836f..bdb82e5 100644 --- a/src/platforms/index.ts +++ b/src/platforms/index.ts @@ -33,4 +33,5 @@ export const errors = { API_RATE_LIMIT: 'API rate limit', EMPTY_PROJECT: 'Empty project', BLOCKED_PROJECT: 'Blocked project', + CONNECTION_BLOCKED: 'Connection blocked', }