-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: conventional store api error handling
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
'use strict' | ||
|
||
// log on files | ||
const logger = require('console-files') | ||
|
||
const ignoreError = response => { | ||
// check response status code | ||
// should ignore some error responses | ||
let { status, data } = response | ||
if (status >= 400 && status < 500) { | ||
switch (status) { | ||
case 403: | ||
// ignore resource limits errors | ||
return true | ||
case 404: | ||
if (data && data.error_code !== 20) { | ||
// resource ID not found ? | ||
// ignore | ||
return true | ||
} | ||
break | ||
} | ||
// must debug | ||
return false | ||
} | ||
} | ||
|
||
module.exports = err => { | ||
// axios error object | ||
// https://github.com/axios/axios#handling-errors | ||
if (!err.appAuthRemoved && !err.appErrorLog) { | ||
// error not treated by App SDK | ||
if (err.response) { | ||
if (ignoreError(err.response)) { | ||
// ignore client error | ||
return | ||
} | ||
err.responseJSON = JSON.stringify(err.response.data) | ||
} | ||
|
||
// debug unexpected response | ||
logger.error(err) | ||
} else if (err.appErrorLog && !err.appErrorLogged) { | ||
// cannot log to app hidden data | ||
// debug app log error | ||
let error = err.appErrorLog | ||
let { response, config } = error | ||
|
||
// handle error response | ||
if (response) { | ||
if (ignoreError(response)) { | ||
return | ||
} | ||
// debug unexpected response | ||
error.configJSON = { | ||
originalRequest: JSON.stringify(err.config), | ||
logRequest: JSON.stringify(config) | ||
} | ||
logger.error(error) | ||
} | ||
} | ||
} |