Skip to content

Commit

Permalink
Merge pull request #168 from frappe/handle-network-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
surajshetty3416 authored Sep 25, 2024
2 parents a514718 + c4f145f commit c34ce58
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/utils/frappeRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,9 @@ export function frappeRequest(options) {
throw e
}
},
transformError: (error) => {
options.onError && options.onError(error)
throw error
},
})
}
33 changes: 20 additions & 13 deletions src/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,26 @@ export function request(_options) {
method: options.method || 'GET',
headers: options.headers,
body,
}).then((response) => {
if (options.transformResponse) {
return options.transformResponse(response, options)
}
if (response.status >= 200 && response.status < 300) {
if (options.responseType === 'json') {
return response.json()
})
.then((response) => {
if (options.transformResponse) {
return options.transformResponse(response, options)
}
if (response.status >= 200 && response.status < 300) {
if (options.responseType === 'json') {
return response.json()
}
return response
} else {
let error = new Error(response.statusText)
error.response = response
throw error
}
})
.catch((error) => {
if (options.transformError) {
return options.transformError(error)
}
return response
} else {
let error = new Error(response.statusText)
error.response = response
throw error
}
})
})
}

0 comments on commit c34ce58

Please sign in to comment.