feat: better error messages

This commit is contained in:
EnixCoda 2020-04-30 22:54:16 +08:00
parent 69506957df
commit 0334b9eb98
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
4 changed files with 37 additions and 24 deletions

View file

@ -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.')

View file

@ -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(

View file

@ -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)
}
}

View file

@ -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',
}