aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/http/resolvePaste.js
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/src/http/resolvePaste.js')
-rw-r--r--frontend/src/http/resolvePaste.js69
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