Skip to content

Commit

Permalink
fix(store): fix mapError bug and add type forApiResponseAction
Browse files Browse the repository at this point in the history
  • Loading branch information
ramfox committed Sep 10, 2019
1 parent 88c70d2 commit c7831ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion app/store/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ interface ApiQuery {
[key: string]: string
}

interface ApiError {
code: number
message?: string
}

export interface ApiResponseAction {
type: string
payload: {
[key: string]: any
}
}

// ApiActionThunk is the return value of an Api action.
// All api actions must return a promise that will be called with their result:
// either a SUCCESS or FAILURE action. This allows callers to chain
Expand Down Expand Up @@ -98,7 +110,8 @@ async function getJSON<T> (url: string, options: FetchOptions): Promise<T> {
const r = await fetch(url, options)
const res = await r.json()
if (res.meta.code !== 200) {
throw new Error(mapError(res.meta.error))
var err: ApiError = { code: res.meta.code, message: mapError(res.meta.error) }
throw err // eslint-disable-line
}
return res as T
}
Expand Down
2 changes: 1 addition & 1 deletion app/store/mapError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const errors: {[key: string]: string} = {

export default function mapError (err: string): string {
var errMessage = err
if (err.includes('@')) {
if (err && err.includes('@')) {
const start = err.indexOf('@')
const errSlice = err.slice(start)
const end = errSlice.indexOf(' ') + start
Expand Down

0 comments on commit c7831ba

Please sign in to comment.