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

master discord_embed_v1.0.14
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,29 +66,31 @@ 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.url.match(/^\/[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]+$/) && ctx.url.length < 7) { if (!ctx.state.error) {
try { if (ctx.url.match(/^\/[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]+$/) && ctx.url.length < 7) {
let id = AlphabeticID.decode(ctx.url.slice(1)) try {
if (id) { let id = AlphabeticID.decode(ctx.url.slice(1))
let res = await ctx.db.safeCallProc('discord_embed.link_get', [id - 3843]) if (id) {
if (res.first.length) { let res = await ctx.db.safeCallProc('discord_embed.link_get', [id - 3843])
videoLink = ctx.state.video = res.first[0].video_link if (res.first.length) {
if (!ctx.state.video.startsWith('https://cdn.discordapp.com') videoLink = ctx.state.video = res.first[0].video_link
&& !ctx.state.video.includes('catbox.') if (!ctx.state.video.startsWith('https://cdn.discordapp.com')
&& ctx.state.video.includes('?')) { && !ctx.state.video.includes('catbox.')
videoLink = this.frontend + '/video/' + ctx.url.slice(1) + '.webm' && ctx.state.video.includes('?')) {
videoLink = this.frontend + '/video/' + ctx.url.slice(1) + '.webm'
}
imageLink = res.first[0].image_link
} else {
ctx.status = 404
} }
imageLink = res.first[0].image_link
} else {
ctx.status = 404
} }
} catch (err) {
ctx.log.error(err, 'Unable to fetch resource ' + ctx.url.slice(1))
ctx.state.error = 'Unknown error while fetching link.'
} }
} catch (err) { } else if (ctx.url !== '/') {
ctx.log.error(err, 'Unable to fetch resource ' + ctx.url.slice(1)) ctx.status = 404
ctx.state.error = 'Unknown error while fetching link.'
} }
} else if (ctx.url !== '/') {
ctx.status = 404
} }
if (videoLink.startsWith('https://cdn.discordapp.com')) { if (videoLink.startsWith('https://cdn.discordapp.com')) {

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",