Skip to content
This repository has been archived by the owner on Jun 13, 2022. It is now read-only.

Commit

Permalink
Fixed response handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGhostHunter committed Jul 10, 2020
1 parent e3463a3 commit 009644e
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions resources/js/larapass.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,25 @@ class Larapass
* Throws the entire response if is not OK (HTTP 2XX).
*
* @param response {Response}
* @returns Promise<any>
* @returns Promise<JSON|ArrayBuffer|ArrayBufferView|Blob|FormData|string|URLSearchParams>
* @throws Response
*/
static #handleResponse(response)
{
if (response.ok) {
return response.json();
if (! response.ok) {
throw response;
}

throw response;
// Here we will do a small trick. Since most of the responses from the server
// are JSON, we will automatically parse the JSON body from the response. If
// it's not JSON, we will push the body verbatim and let the dev handle it.
return new Promise(resolve => {
if (response) {
response.json().then(json => resolve(json)).catch(() => resolve(response.body))
} else {
resolve(response.body)
}
})
}

/**
Expand Down

0 comments on commit 009644e

Please sign in to comment.