discord_embed: Better error message on 400 errors (hopefully)
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded

This commit is contained in:
Jonatan Nilsson 2023-01-26 09:33:07 +00:00
parent 97b54d23c4
commit 23580dea3f
2 changed files with 58 additions and 21 deletions

View file

@ -1,4 +1,5 @@
import path from 'path' import path from 'path'
import { HttpError } from 'flaska'
import Parent from '../base/serve.mjs' import Parent from '../base/serve.mjs'
import fs from 'fs/promises' import fs from 'fs/promises'
import fsSync from 'fs' import fsSync from 'fs'
@ -22,6 +23,40 @@ export default class ServeHandler extends Parent {
] }) ] })
} }
register(server) {
super.register(server)
server.flaska.onerror(this.serveError.bind(this))
}
serveError(err, ctx) {
ctx.log.error(err)
if (err instanceof HttpError) {
ctx.status = err.status
ctx.state.error = err.message
} else {
ctx.status = 500
ctx.state.error = 'Unknown error occured'
}
let videoLink = ctx.query.get('v') || ''
let imageLink = ctx.query.get('i') || ''
ctx.body = this.template({
videoLink: videoLink,
imageLink: imageLink,
error: ctx.state.error || '',
inputVideo: ctx.state.video || videoLink || '',
inputImage: ctx.state.image || imageLink || '',
siteUrl: this.frontend + ctx.url,
siteUrlBase: this.frontend + '/',
version: this.version,
nonce: ctx.state.nonce,
in_debug: config.get('NODE_ENV') === 'development' && false,
})
ctx.type = 'text/html; charset=utf-8'
}
async serveIndex(ctx) { async serveIndex(ctx) {
if (config.get('NODE_ENV') === 'development') { if (config.get('NODE_ENV') === 'development') {
let indexFile = await fs.readFile(path.join(this.root, 'index.html')) let indexFile = await fs.readFile(path.join(this.root, 'index.html'))
@ -31,6 +66,7 @@ export default class ServeHandler extends Parent {
let videoLink = ctx.query.get('v') || '' let videoLink = ctx.query.get('v') || ''
let imageLink = ctx.query.get('i') || '' let imageLink = ctx.query.get('i') || ''
if (!ctx.state.error) {
if (ctx.url.match(/^\/[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]+$/) && ctx.url.length < 7) { if (ctx.url.match(/^\/[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]+$/) && ctx.url.length < 7) {
try { try {
let id = AlphabeticID.decode(ctx.url.slice(1)) let id = AlphabeticID.decode(ctx.url.slice(1))
@ -55,6 +91,7 @@ export default class ServeHandler extends Parent {
} else if (ctx.url !== '/') { } else if (ctx.url !== '/') {
ctx.status = 404 ctx.status = 404
} }
}
if (videoLink.startsWith('https://cdn.discordapp.com')) { if (videoLink.startsWith('https://cdn.discordapp.com')) {
videoLink = videoLink.replace('https://cdn.discordapp.com', 'https://discordproxy.nfp.is') videoLink = videoLink.replace('https://cdn.discordapp.com', 'https://discordproxy.nfp.is')

View file

@ -1,6 +1,6 @@
{ {
"name": "discord_embed", "name": "discord_embed",
"version": "1.0.13", "version": "1.0.14",
"port": 4120, "port": 4120,
"description": "AV1 discord server embed helper", "description": "AV1 discord server embed helper",
"main": "index.js", "main": "index.js",
@ -37,7 +37,7 @@
"dependencies": { "dependencies": {
"bunyan-lite": "^1.2.1", "bunyan-lite": "^1.2.1",
"dot": "^2.0.0-beta.1", "dot": "^2.0.0-beta.1",
"flaska": "^1.3.0", "flaska": "^1.3.2",
"formidable": "^1.2.6", "formidable": "^1.2.6",
"ioredis": "^5.2.3", "ioredis": "^5.2.3",
"msnodesqlv8": "^2.4.7", "msnodesqlv8": "^2.4.7",