Skip to content

Commit ddddcff

Browse files
committed
Expose HttpError to plugins
This will let them throw and show nice errors more easily.
1 parent 6ca4236 commit ddddcff

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

src/node/plugin.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as fs from "fs"
44
import * as path from "path"
55
import * as semver from "semver"
66
import * as pluginapi from "../../typings/pluginapi"
7+
import { HttpCode, HttpError } from "../common/http"
78
import { version } from "./constants"
89
import { replaceTemplates } from "./http"
910
import { proxy } from "./proxy"
@@ -22,6 +23,8 @@ require("module")._load = function (request: string, parent: object, isMain: boo
2223
return {
2324
express,
2425
field,
26+
HttpCode,
27+
HttpError,
2528
Level,
2629
proxy,
2730
replaceTemplates,

test/plugin.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as express from "express"
44
import * as fs from "fs"
55
import { describe } from "mocha"
66
import * as path from "path"
7+
import { HttpCode } from "../src/common/http"
78
import { PluginAPI } from "../src/node/plugin"
89
import * as apps from "../src/node/routes/apps"
910
import * as httpserver from "./httpserver"
@@ -83,4 +84,9 @@ describe("plugin", () => {
8384
ws.terminate()
8485
assert.strictEqual(message, "hello")
8586
})
87+
88+
it("/test-plugin/error", async () => {
89+
const resp = await s.fetch("/test-plugin/error")
90+
assert.equal(HttpCode.LargePayload, resp.status)
91+
})
8692
})

test/test-plugin/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export const plugin: cs.Plugin = {
1919
r.get("/goland/icon.svg", (_, res) => {
2020
res.sendFile(fspath.resolve(__dirname, "../public/icon.svg"))
2121
})
22+
r.get("/error", () => {
23+
throw new cs.HttpError("error", cs.HttpCode.LargePayload)
24+
})
2225
return r
2326
},
2427

typings/pluginapi.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ import Websocket from "ws"
8282
* ]
8383
*/
8484

85+
export enum HttpCode {
86+
Ok = 200,
87+
Redirect = 302,
88+
NotFound = 404,
89+
BadRequest = 400,
90+
Unauthorized = 401,
91+
LargePayload = 413,
92+
ServerError = 500,
93+
}
94+
95+
export declare class HttpError extends Error {
96+
constructor(message: string, status: HttpCode, details?: object)
97+
}
98+
8599
export interface WebsocketRequest extends express.Request {
86100
ws: net.Socket
87101
head: Buffer

0 commit comments

Comments
 (0)