From 67be3acc6ee23b1894773b9c44077bed8553c91a Mon Sep 17 00:00:00 2001 From: "Indrawan I." Date: Thu, 14 May 2026 09:56:52 +0700 Subject: [PATCH] fix: proper error handler (#23) * re use legacy errors * 8.3.6-alpha --- package.json | 2 +- src/LustPress.ts | 13 +---- src/index.ts | 138 +++++++++++++++++++++++------------------------ 3 files changed, 71 insertions(+), 82 deletions(-) diff --git a/package.json b/package.json index 77337a9..38732d3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lustpress", - "version": "8.3.5-alpha", + "version": "8.3.6-alpha", "description": "RESTful and experimental API for PH and other R18 websites", "main": "src/index.ts", "scripts": { diff --git a/src/LustPress.ts b/src/LustPress.ts index 96728c7..2041b33 100644 --- a/src/LustPress.ts +++ b/src/LustPress.ts @@ -88,18 +88,7 @@ class LustPress { const domain = new URL(url).hostname; const headers: Record = { - "User-Agent": isPornhub ? this.browserUA : this.useragent, - "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", - "Accept-Language": "en-US,en;q=0.9", - "Accept-Encoding": "gzip, deflate, br", - "Sec-Ch-Ua": '"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"', - "Sec-Ch-Ua-Mobile": "?0", - "Sec-Ch-Ua-Platform": '"Windows"', - "Sec-Fetch-Dest": "document", - "Sec-Fetch-Mode": "navigate", - "Sec-Fetch-Site": "none", - "Sec-Fetch-User": "?1", - "Upgrade-Insecure-Requests": "1" + "User-Agent": isPornhub ? this.browserUA : this.useragent }; if (isPornhub) { diff --git a/src/index.ts b/src/index.ts index 3b5312b..1de04c8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,69 +1,69 @@ -import { Elysia } from "elysia"; -import { cors } from "@elysiajs/cors"; -import { swagger } from "@elysiajs/swagger"; -import { lust } from "./LustPress"; -import { scrapeRoutes } from "./router/endpoint"; -import pkg from "../package.json"; - -const app = new Elysia() - .use(cors()) - .use( - swagger({ - path: "/docs", - documentation: { - info: { - title: "Lustpress API", - version: pkg.version, - description: pkg.description, - }, - }, - }) - ) - .get("/", async () => ({ - success: true, - playground: "https://sinkaroid.github.io/lustpress", - endpoint: - "https://github.com/sinkaroid/lustpress/blob/master/README.md#routing", - date: new Date().toLocaleString(), - rss: lust.currentProccess().rss, - heap: lust.currentProccess().heap, - server: await lust.getServer(), - version: pkg.version, - })) - .onError(({ code, error, set }) => { - if (code === "NOT_FOUND") { - set.status = 404; - return { - success: false, - message: (error as Error).message || "Not Found", - }; - } - if (code === "VALIDATION") { - set.status = 400; - return { - success: false, - message: JSON.parse((error as Error).message)[0]?.message || "Validation Error", - }; - } - if (code === "UNKNOWN") { - set.status = 400; - return { - success: false, - message: (error as Error).message, - }; - } - set.status = 500; - return { - success: false, - message: (error as Error).message || "Internal Server Error", - stack: (error as Error).stack, - }; - }) - .use(scrapeRoutes) - .listen(process.env.PORT || 3000); - -console.log( - `Lustpress is running at ${app.server?.hostname}:${app.server?.port}` -); - -export type App = typeof app; \ No newline at end of file +import { Elysia } from "elysia"; +import { cors } from "@elysiajs/cors"; +import { swagger } from "@elysiajs/swagger"; +import { lust } from "./LustPress"; +import { scrapeRoutes } from "./router/endpoint"; +import pkg from "../package.json"; + +const app = new Elysia() + .use(cors()) + .use( + swagger({ + path: "/docs", + documentation: { + info: { + title: "Lustpress API", + version: pkg.version, + description: pkg.description, + }, + }, + }) + ) + .get("/", async () => ({ + success: true, + playground: "https://sinkaroid.github.io/lustpress", + endpoint: + "https://github.com/sinkaroid/lustpress/blob/master/README.md#routing", + date: new Date().toLocaleString(), + rss: lust.currentProccess().rss, + heap: lust.currentProccess().heap, + server: await lust.getServer(), + version: pkg.version, + })) + .onError(({ code, error, set }) => { + if (code === "NOT_FOUND") { + set.status = 404; + return { + success: false, + message: (error as Error).message || "Not Found", + }; + } + if (code === "VALIDATION") { + set.status = 400; + return { + success: false, + message: JSON.parse((error as Error).message)[0]?.message || "Validation Error", + }; + } + if (code === "UNKNOWN") { + set.status = 400; + return { + success: false, + message: (error as Error).message, + }; + } + set.status = 500; + return { + success: false, + message: (error as Error).message || "Internal Server Error", + stack: (error as Error).stack, + }; + }) + .use(scrapeRoutes) // reuse legacy error handling + .listen(process.env.PORT || 3000); + +console.log( + `Lustpress is running at ${app.server?.hostname}:${app.server?.port}` +); + +export type App = typeof app;