diff options
| author | secondl1ght <[email protected]> | 2022-08-02 13:24:51 -0600 |
|---|---|---|
| committer | secondl1ght <[email protected]> | 2022-08-02 13:24:51 -0600 |
| commit | a3e77829b6ddf046fdda6e3b5a542b3d52c9b205 (patch) | |
| tree | 6024c3bae16ac611a3aabf99f424cb83307430c5 /src/lib/utils.js | |
| parent | 8c6649ad645dd335e4a3b474406df44152d17921 (diff) | |
| parent | 0676ad3f9afa2036dccce7165478ea0ba09dae1d (diff) | |
| download | coinos-ui-a3e77829b6ddf046fdda6e3b5a542b3d52c9b205.tar.gz coinos-ui-a3e77829b6ddf046fdda6e3b5a542b3d52c9b205.zip | |
Merge branch 'master' of github.com:coinos/coinos-ui-v2
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 6b728152..a812c136 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -14,7 +14,7 @@ export const get = (url, headers = { accept: 'application/json' }) => try { return JSON.parse(body); } catch (e) { - throw Error(body); + throw new Error(body); } }); @@ -23,7 +23,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); |
