Add HTTP status to MatrixServerError
This commit is contained in:
@@ -23,7 +23,7 @@ async function getAndConvertEmoji(mxc) {
|
||||
const res = await api.getMedia(mxc, {signal: abortController.signal})
|
||||
if (res.status !== 200) {
|
||||
const root = await res.json()
|
||||
throw new mreq.MatrixServerError(root, {mxc})
|
||||
throw new mreq.MatrixServerError(root, res.status, {mxc})
|
||||
}
|
||||
const readable = stream.Readable.fromWeb(res.body)
|
||||
return emojiSheetConverter.convertImageStream(readable, () => {
|
||||
|
||||
@@ -22,7 +22,7 @@ async function getAndResizeSticker(mxc) {
|
||||
const res = await api.getMedia(mxc)
|
||||
if (res.status !== 200) {
|
||||
const root = await res.json()
|
||||
throw new mreq.MatrixServerError(root, {mxc})
|
||||
throw new mreq.MatrixServerError(root, res.status, {mxc})
|
||||
}
|
||||
|
||||
const streamIn = Readable.fromWeb(res.body)
|
||||
|
||||
+9
-6
@@ -9,9 +9,12 @@ const {reg} = require("./read-registration.js")
|
||||
const baseUrl = `${reg.ooye.server_origin}/_matrix`
|
||||
|
||||
class MatrixServerError extends Error {
|
||||
constructor(data, opts) {
|
||||
/** @param {number} httpStatus} */
|
||||
constructor(data, httpStatus, opts) {
|
||||
super(data.error || data.errcode)
|
||||
this.data = data
|
||||
/** @type {number} */
|
||||
this.httpStatus = httpStatus
|
||||
/** @type {string} */
|
||||
this.errcode = data.errcode
|
||||
this.opts = opts
|
||||
@@ -44,11 +47,11 @@ async function _convertBody(body) {
|
||||
async function makeMatrixServerError(res, opts = {}) {
|
||||
delete opts.headers?.["Authorization"]
|
||||
if (res.headers.get("content-type") === "application/json") {
|
||||
return new MatrixServerError(await res.json(), opts)
|
||||
return new MatrixServerError(await res.json(), res.status, opts)
|
||||
} else if (res.headers.get("content-type")?.startsWith("text/")) {
|
||||
return new MatrixServerError({errcode: "CX_SERVER_ERROR", error: `Server returned HTTP status ${res.status}`, message: await res.text()}, opts)
|
||||
return new MatrixServerError({errcode: "CX_SERVER_ERROR", error: `Server returned HTTP status ${res.status}`, message: await res.text()}, res.status, opts)
|
||||
} else {
|
||||
return new MatrixServerError({errcode: "CX_SERVER_ERROR", error: `Server returned HTTP status ${res.status}`, content_type: res.headers.get("content-type")}, opts)
|
||||
return new MatrixServerError({errcode: "CX_SERVER_ERROR", error: `Server returned HTTP status ${res.status}`, content_type: res.headers.get("content-type")}, res.status, opts)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,12 +81,12 @@ async function mreq(method, url, bodyIn, extra = {}) {
|
||||
var root = JSON.parse(text)
|
||||
} catch (e) {
|
||||
delete opts.headers?.["Authorization"]
|
||||
throw new MatrixServerError(text, {baseUrl, url, ...opts})
|
||||
throw new MatrixServerError(text, res.status, {baseUrl, url, ...opts})
|
||||
}
|
||||
|
||||
if (!res.ok || root.errcode) {
|
||||
delete opts.headers?.["Authorization"]
|
||||
throw new MatrixServerError(root, {baseUrl, url, ...opts})
|
||||
throw new MatrixServerError(root, res.status, {baseUrl, url, ...opts})
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user