Expose HttpError to plugins
This will let them throw and show nice errors more easily.
This commit is contained in:
parent
22d194515a
commit
c78f56b334
@ -4,6 +4,7 @@ import * as fs from "fs"
|
|||||||
import * as path from "path"
|
import * as path from "path"
|
||||||
import * as semver from "semver"
|
import * as semver from "semver"
|
||||||
import * as pluginapi from "../../typings/pluginapi"
|
import * as pluginapi from "../../typings/pluginapi"
|
||||||
|
import { HttpCode, HttpError } from "../common/http"
|
||||||
import { version } from "./constants"
|
import { version } from "./constants"
|
||||||
import { replaceTemplates } from "./http"
|
import { replaceTemplates } from "./http"
|
||||||
import { proxy } from "./proxy"
|
import { proxy } from "./proxy"
|
||||||
@ -22,6 +23,8 @@ require("module")._load = function (request: string, parent: object, isMain: boo
|
|||||||
return {
|
return {
|
||||||
express,
|
express,
|
||||||
field,
|
field,
|
||||||
|
HttpCode,
|
||||||
|
HttpError,
|
||||||
Level,
|
Level,
|
||||||
proxy,
|
proxy,
|
||||||
replaceTemplates,
|
replaceTemplates,
|
||||||
|
@ -2,6 +2,7 @@ import { logger } from "@coder/logger"
|
|||||||
import * as express from "express"
|
import * as express from "express"
|
||||||
import * as fs from "fs"
|
import * as fs from "fs"
|
||||||
import * as path from "path"
|
import * as path from "path"
|
||||||
|
import { HttpCode } from "../src/common/http"
|
||||||
import { PluginAPI } from "../src/node/plugin"
|
import { PluginAPI } from "../src/node/plugin"
|
||||||
import * as apps from "../src/node/routes/apps"
|
import * as apps from "../src/node/routes/apps"
|
||||||
import * as httpserver from "./httpserver"
|
import * as httpserver from "./httpserver"
|
||||||
@ -81,4 +82,9 @@ describe("plugin", () => {
|
|||||||
ws.terminate()
|
ws.terminate()
|
||||||
expect(message).toBe("hello")
|
expect(message).toBe("hello")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("/test-plugin/error", async () => {
|
||||||
|
const resp = await s.fetch("/test-plugin/error")
|
||||||
|
expect(resp.status).toBe(HttpCode.LargePayload)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -19,6 +19,9 @@ export const plugin: cs.Plugin = {
|
|||||||
r.get("/goland/icon.svg", (_, res) => {
|
r.get("/goland/icon.svg", (_, res) => {
|
||||||
res.sendFile(fspath.resolve(__dirname, "../public/icon.svg"))
|
res.sendFile(fspath.resolve(__dirname, "../public/icon.svg"))
|
||||||
})
|
})
|
||||||
|
r.get("/error", () => {
|
||||||
|
throw new cs.HttpError("error", cs.HttpCode.LargePayload)
|
||||||
|
})
|
||||||
return r
|
return r
|
||||||
},
|
},
|
||||||
|
|
||||||
|
14
typings/pluginapi.d.ts
vendored
14
typings/pluginapi.d.ts
vendored
@ -82,6 +82,20 @@ import Websocket from "ws"
|
|||||||
* ]
|
* ]
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
export enum HttpCode {
|
||||||
|
Ok = 200,
|
||||||
|
Redirect = 302,
|
||||||
|
NotFound = 404,
|
||||||
|
BadRequest = 400,
|
||||||
|
Unauthorized = 401,
|
||||||
|
LargePayload = 413,
|
||||||
|
ServerError = 500,
|
||||||
|
}
|
||||||
|
|
||||||
|
export declare class HttpError extends Error {
|
||||||
|
constructor(message: string, status: HttpCode, details?: object)
|
||||||
|
}
|
||||||
|
|
||||||
export interface WebsocketRequest extends express.Request {
|
export interface WebsocketRequest extends express.Request {
|
||||||
ws: net.Socket
|
ws: net.Socket
|
||||||
head: Buffer
|
head: Buffer
|
||||||
|
Reference in New Issue
Block a user