diff options
| author | jackyzha0 <[email protected]> | 2021-04-11 13:50:20 -0700 |
|---|---|---|
| committer | jackyzha0 <[email protected]> | 2021-04-11 13:50:20 -0700 |
| commit | d58c7a3ad5dc83a08e040a855f158f8c6c09e154 (patch) | |
| tree | ec2f295f0816858915702c1f4e67973c2e4976fa /frontend/src/http/resolvePaste.js | |
| parent | refactor error handling in pasteinfo (diff) | |
| download | ctrl-v-d58c7a3ad5dc83a08e040a855f158f8c6c09e154.tar.xz ctrl-v-d58c7a3ad5dc83a08e040a855f158f8c6c09e154.zip | |
password resolution, dynamic head
Diffstat (limited to 'frontend/src/http/resolvePaste.js')
| -rw-r--r-- | frontend/src/http/resolvePaste.js | 69 |
1 files changed, 36 insertions, 33 deletions
diff --git a/frontend/src/http/resolvePaste.js b/frontend/src/http/resolvePaste.js index 8d40cbe..aa4f8b6 100644 --- a/frontend/src/http/resolvePaste.js +++ b/frontend/src/http/resolvePaste.js @@ -2,42 +2,45 @@ import {useEffect, useState} from 'react' import {fetchPaste, fmtDateStr} from './shared' import {LANGS} from "../components/renderers/Code"; -const resolvePaste = (id, password = "") => { - const response = { - data: { - title: '', - content: '', - language: LANGS.detect, - expiry: '', - }, - unauthorized: false, - error: '', - } - return fetchPaste(id, password) - .then(resp => { - const data = resp.data - response.data = { - ...data, - expiry: fmtDateStr(data.expiry) - } - return response - }) - .catch(error => { - const resp = error.response - if (!resp) { - response.error = 'network error' - return - } +export const defaultResponse = { + data: { + title: '', + content: '', + language: LANGS.detect, + expiry: '', + }, + unauthorized: false, + error: '', +} - if (resp.status === 401) { - response.error = 'unauthorized' - response.unauthorized = true - return - } +const resolvePaste = async (id, password = "") => { + const response = {...defaultResponse} + try { + return await fetchPaste(id, password) + .then(resp => { + const data = resp.data + response.data = { + ...data, + expiry: fmtDateStr(data.expiry) + } + return response + }) + } catch (err) { + const resp = err.response + if (!resp) { + response.error = 'network error' + return response + } - response.error = `${resp.status}: ${resp.data}` + if (resp.status === 401) { + response.error = 'unauthorized' + response.unauthorized = true return response - }) + } + + response.error = `${resp.status}: ${resp.data}` + return response + } } export default resolvePaste
\ No newline at end of file |