Skip to content

Commit

Permalink
feat(tslua-http-api): add error tracking within Application and suppo…
Browse files Browse the repository at this point in the history
…rt for JSON encode errors
  • Loading branch information
🎲 committed Dec 2, 2024
1 parent 0923a66 commit 43e66f0
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 61 deletions.
4 changes: 4 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"ignore": ["node_modules", "docs"],
"maxSize": 5000000
},
"formatter": {
"enabled": true,
"lineEnding": "crlf"
},
"vcs": {
"enabled": true,
"clientKind": "git",
Expand Down
6 changes: 3 additions & 3 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "0.30.11"
}
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "0.30.11"
}
6 changes: 1 addition & 5 deletions packages/tslua-base64/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
"name": "@flying-dice/tslua-base64",
"version": "0.30.11",
"description": "Base64 library with typings",
"keywords": [
"lua",
"dcs",
"base64"
],
"keywords": ["lua", "dcs", "base64"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down
6 changes: 1 addition & 5 deletions packages/tslua-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
"name": "@flying-dice/tslua-common",
"version": "0.30.11",
"description": "Common Utils Library",
"keywords": [
"lua",
"dcs",
"logger"
],
"keywords": ["lua", "dcs", "logger"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down
6 changes: 1 addition & 5 deletions packages/tslua-dcs-gui-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
"name": "@flying-dice/tslua-dcs-gui-types",
"version": "0.30.11",
"description": "Typescript type definitions for DCS GUI environment",
"keywords": [
"lua",
"dcs",
"gui"
],
"keywords": ["lua", "dcs", "gui"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down
6 changes: 1 addition & 5 deletions packages/tslua-dcs-mission-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
"name": "@flying-dice/tslua-dcs-mission-types",
"version": "0.30.11",
"description": "Typescript type definitions for DCS",
"keywords": [
"lua",
"dcs",
"mission"
],
"keywords": ["lua", "dcs", "mission"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down
10 changes: 2 additions & 8 deletions packages/tslua-http-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
"name": "@flying-dice/tslua-http-api",
"version": "0.30.11",
"description": "LUA HTTP API Server written in Typescript Transpiled to lua using TypeScriptToLua",
"keywords": [
"lua",
"http",
"api"
],
"keywords": ["lua", "http", "api"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand All @@ -27,9 +23,7 @@
},
"nodemonConfig": {
"verbose": true,
"watch": [
"dist"
],
"watch": ["dist"],
"ext": "lua",
"exec": "npm run dev"
},
Expand Down
21 changes: 16 additions & 5 deletions packages/tslua-http-api/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class AppHttpRequest<PARAMS = any, QUERY = any, BODY = any> {
}

constructor(
private readonly app: Application,
public readonly req: HttpRequest,
pathParams: Record<string, string>,
) {
Expand Down Expand Up @@ -129,7 +130,10 @@ export class AppHttpRequest<PARAMS = any, QUERY = any, BODY = any> {
export class AppHttpResponse<RESPONSE = any> {
protected logger = new Logger("AppHttpResponse");

constructor(public readonly res: HttpResponse) {}
constructor(
private readonly app: Application,
public readonly res: HttpResponse,
) {}

/**
* Sets the HTTP status code.
Expand Down Expand Up @@ -167,8 +171,13 @@ export class AppHttpResponse<RESPONSE = any> {
*/
json<T = RESPONSE>(value: T): this {
this.res.headers["Content-Type"] = "application/json";
this.res.body = json.encode(value);
return this;
try {
this.res.body = json.encode(value);
return this;
} catch (e) {
this.app.errors.push({ error: e as Error, value });
throw e;
}
}

/**
Expand Down Expand Up @@ -202,6 +211,8 @@ export type AppErrorMiddleware = <T = Error>(
* A class representing a web application, extending the functionality of HttpServer.
*/
export class Application extends HttpServer {
public errors: { error: Error; [key: string]: any }[] = [];

protected requestHandlers: {
route: string;
method?: string;
Expand Down Expand Up @@ -323,8 +334,8 @@ export class Application extends HttpServer {
res.status = HttpStatus.OK;
}

const appRequest = new AppHttpRequest(req, {});
const appResponse = new AppHttpResponse(res);
const appRequest = new AppHttpRequest(this, req, {});
const appResponse = new AppHttpResponse(this, res);

try {
const runStackItem = (idx: number) => {
Expand Down
5 changes: 1 addition & 4 deletions packages/tslua-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
"name": "@flying-dice/tslua-http",
"version": "0.30.11",
"description": "LUA HTTP Server written in Typescript Transpiled to lua using TypeScriptToLua",
"keywords": [
"lua",
"http"
],
"keywords": ["lua", "http"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down
6 changes: 1 addition & 5 deletions packages/tslua-luatest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
"name": "@flying-dice/tslua-luatest",
"version": "0.30.11",
"description": "tslua testing library",
"keywords": [
"lua",
"dcs",
"test"
],
"keywords": ["lua", "dcs", "test"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down
7 changes: 1 addition & 6 deletions packages/tslua-openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
"name": "@flying-dice/tslua-openapi",
"version": "0.30.11",
"description": "Re-export of https://github.com/metadevpro/openapi3-ts/ library for tslua",
"keywords": [
"lua",
"http",
"api",
"openapi"
],
"keywords": ["lua", "http", "api", "openapi"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down
7 changes: 1 addition & 6 deletions packages/tslua-rxi-json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
"description": "rxi-json library with typings",
"types": "./index.d.ts",
"main": "./index",
"keywords": [
"lua",
"dcs",
"json",
"rxi/json"
],
"keywords": ["lua", "dcs", "json", "rxi/json"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down
6 changes: 2 additions & 4 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:recommended"
]
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": ["config:recommended"]
}

0 comments on commit 43e66f0

Please sign in to comment.