diff options
| author | Adam Soltys <[email protected]> | 2022-08-02 10:06:57 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2022-08-02 10:06:57 -0700 |
| commit | 0676ad3f9afa2036dccce7165478ea0ba09dae1d (patch) | |
| tree | b3bf305befd7afb214af4890326b03ebeea1ebae /src/lib/utils.js | |
| parent | c47a1a57c11e1a23ab00e382d79383360263ba62 (diff) | |
| download | coinos-ui-0676ad3f9afa2036dccce7165478ea0ba09dae1d.tar.gz coinos-ui-0676ad3f9afa2036dccce7165478ea0ba09dae1d.zip | |
catch login and registration errors
Diffstat (limited to 'src/lib/utils.js')
| -rw-r--r-- | src/lib/utils.js | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/src/lib/utils.js b/src/lib/utils.js index db8a490a..669b2b5e 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -13,7 +13,7 @@ export const get = (url, headers = { accept: 'application/json' }) => try { return JSON.parse(body); } catch (e) { - throw Error(body); + throw new Error(body); } }); @@ -22,7 +22,20 @@ export const post = (url, body) => method: 'POST', body: JSON.stringify(body), headers: { accept: 'application/json', 'content-type': 'application/json' } - }).then((r) => r.json()); + }) + .then((r) => r.text()) + .then((body) => { + try { + body = JSON.parse(body); + } catch (e) { + throw new Error(body); + } + + if (body instanceof Error) throw body; + if (body.name === 'Error') throw new Error(body.message); + + return body; + }); export const copy = (text) => { navigator.clipboard.writeText(text); |
