Skip to content

Commit

Permalink
setup error middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuRA committed Jan 24, 2025
1 parent 235d0ae commit bdcd892
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 10 deletions.
3 changes: 2 additions & 1 deletion @xen-orchestra/rest-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"inversify": "^6.2.1",
"inversify-binding-decorators": "^4.0.0",
"swagger-ui-express": "^5.0.1",
"tsoa": "^6.6.0"
"tsoa": "^6.6.0",
"xo-common": "^0.8.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Request, Route, Security } from 'tsoa'
import { Controller, Get, Request, Route } from 'tsoa'
import DashboardService from './dashboard.service.js'
import { inject } from 'inversify'
import { provideSingleton } from '../ioc/helper.js'
Expand Down Expand Up @@ -26,7 +26,6 @@ export class DashboardController extends Controller {
this.#dashboardService = dashboardService
}

@Security('token', ['admin'])
@Get()
public async getDashboard(@Request() req: ExReq): Promise<Dashboard> {
const resp = req.res!
Expand Down
10 changes: 6 additions & 4 deletions @xen-orchestra/rest-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { XapiXoObject, XoApp } from './xoApp.type.js'
import { EventEmitter } from 'events'
import DashboardService from './dashboard/dashboard.service.js'
import { iocContainer } from './ioc/ioc.js'
import { XoServer } from './servers/server.type.js'
import { errorHandler } from './middleware/error.middleware.js'

class RestApi {
#sseClients: Map<symbol, Response> = new Map()
Expand All @@ -17,8 +17,8 @@ class RestApi {
authenticateUser
getObject
getObjects
getServers
getServer
getServers: XoApp['getAllXenServers']
getServer: XoApp['getXenServer']
getObjectsByType

constructor(xoApp: XoApp) {
Expand All @@ -31,7 +31,7 @@ class RestApi {
this.getObject = xoApp.getObject.bind(xoApp)
this.getObjects = xoApp.getObjects.bind(xoApp)
this.getServers = () => xoApp.getAllXenServers()
this.getServer = (id: XoServer['id']) => xoApp.getXenServer(id)
this.getServer = id => xoApp.getXenServer(id)

// helpers
this.getObjectsByType = <T extends keyof typeof xoApp.objects.indexes.type>(type: T) =>
Expand Down Expand Up @@ -120,6 +120,8 @@ export default function setupRestApi(express: Express, xoApp: XoApp) {
express.use('/rest/v1/api-doc', swaggerUi.serve, swaggerUi.setup(swaggerOpenApiSpec))
RegisterRoutes(express)

express.use(errorHandler)

// in order to create the instance of the service (and start to listen for dashboard changes)
iocContainer.get(DashboardService)
}
14 changes: 14 additions & 0 deletions @xen-orchestra/rest-api/src/middleware/error.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { noSuchObject } from 'xo-common/api-errors.js'

import { NextFunction, Request, Response } from 'express'

export function errorHandler(err: Error, _req: Request, res: Response, next: NextFunction) {
if (noSuchObject.is(err)) {
res.status(404).json({ error: err.message })
return next()
}

res.json({ error: err.message })

next()
}
2 changes: 2 additions & 0 deletions @xen-orchestra/rest-api/src/servers/server.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export class ServersController extends XoController<XoServer> {
constructor() {
super('server')
}

@Security('token', ['admin'])
@Get()
public getServers(): Promise<string[]> {
return this.getObjectIds()
Expand Down
1 change: 1 addition & 0 deletions @xen-orchestra/rest-api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"module": "NodeNext",
"outDir": "./dist",
"rootDir": "./src",
"allowJs": true,

/* Strict Type-Checking Options */
"strict": true,
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4668,9 +4668,9 @@
undici-types "~6.19.2"

"@types/node@^22.10.6":
version "22.10.9"
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.10.9.tgz#b62b5e8485b9b412262466209280405525320108"
integrity sha512-Ir6hwgsKyNESl/gLOcEz3krR4CBGgliDqBQ2ma4wIhEx0w+xnoeTq3tdrNw15kU3SxogDjOgv9sqdtLW8mIHaw==
version "22.10.10"
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.10.10.tgz#85fe89f8bf459dc57dfef1689bd5b52ad1af07e6"
integrity sha512-X47y/mPNzxviAGY5TcYPtYL8JsY3kAq2n8fMmKoRCxq/c4v4pyGNCzM2R6+M5/umG4ZfHuT+sgqDYqWc9rJ6ww==
dependencies:
undici-types "~6.20.0"

Expand Down

0 comments on commit bdcd892

Please sign in to comment.