mirror of
https://github.com/Blah-IM/blahrs.git
synced 2025-07-08 15:15:33 +00:00
Define error response format and refactor error handling
This commit is contained in:
parent
4ceffe3f31
commit
4937502d4c
6 changed files with 341 additions and 195 deletions
|
@ -177,12 +177,12 @@ async function connectRoom(url) {
|
|||
},
|
||||
},
|
||||
)
|
||||
.then((resp) => {
|
||||
if (!resp.ok) throw new Error(`status ${resp.status} ${resp.statusText}`);
|
||||
return resp.json();
|
||||
.then(async (resp) => {
|
||||
return [resp.status, await resp.json()];
|
||||
})
|
||||
// TODO: This response format is to-be-decided.
|
||||
.then(async (json) => {
|
||||
.then(async ([status, json]) => {
|
||||
if (status !== 200) throw new Error(`status ${status}: ${json.error.message}`);
|
||||
const [{ title }, items] = json
|
||||
document.title = `room: ${title}`
|
||||
items.reverse();
|
||||
|
@ -256,7 +256,10 @@ async function postChat(text) {
|
|||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
if (!resp.ok) throw new Error(`status ${resp.status} ${resp.statusText}`);
|
||||
if (!resp.ok) {
|
||||
const errResp = await resp.json();
|
||||
throw new Error(`status ${resp.status}: ${errResp.error.message}`);
|
||||
}
|
||||
chatInput.value = '';
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue