Skip to content

Commit

Permalink
feat: conventional store api error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
leomp12 committed Jun 14, 2019
1 parent 6b8a71a commit bcde87e
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions app/lib/ecom-api/error-handling.js
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)
}
}
}

0 comments on commit bcde87e

Please sign in to comment.