Skip to content

Commit

Permalink
handle XapiError
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuRA committed Feb 28, 2025
1 parent 5cfedeb commit 107de36
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions @xen-orchestra/rest-api/.USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Foo extends Controller {
@Example(['foo', 'bar'])
@Get('{id}')
@Security('*')
@SuccessResponse(202)
@Response(404)
getFoo(@Path() id: string) {
return this.getFoo(id)
Expand Down
1 change: 1 addition & 0 deletions @xen-orchestra/rest-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Foo extends Controller {
@Example(['foo', 'bar'])
@Get('{id}')
@Security('*')
@SuccessResponse(202)
@Response(404)
getFoo(@Path() id: string) {
return this.getFoo(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function genericErrorHandler(error: unknown, req: Request, res: R
return
}

const responseError: { error: string; info?: string } = { error: error.message }
if (noSuchObject.is(error)) {
res.status(404)
} else if (unauthorized.is(error) || forbiddenOperation.is(error) || featureUnauthorized.is(error)) {
Expand All @@ -32,11 +33,14 @@ export default function genericErrorHandler(error: unknown, req: Request, res: R
} else if (notImplemented.is(error)) {
res.status(501)
} else {
if (error.name === 'XapiError') {
responseError.info = 'This is a XenServer/XCP-ng error, not an XO error'
}
res.status(500)
log.error(error)
}

log.info(`[${req.method}] ${req.path} (${res.statusCode})`)

res.json({ error: error.message })
res.json(responseError)
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const taskLocation = 'rest/v0/tasks/0m7kl0j9l'
export const taskLocation = '/rest/v0/tasks/0m7kl0j9l'
2 changes: 1 addition & 1 deletion @xen-orchestra/rest-api/src/vms/vm.controller.mts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ export class VmController extends XapiXoController<XoVm> {
}

/**
*
* @example id "f07ab729-c0e8-721c-45ec-f11276377030"
*/
@Example(taskLocation)
@Post('{id}/actions/start')
@SuccessResponse(202, 'Action executed asynchronously')
@Response(204, 'No content')
@Response(404, 'VM not found')
@Response(500, 'Internal server error, XenServer/XCP-ng error')
async startVm(@Path() id: string, @Query() sync?: boolean) {
const vmId = id as XoVm['id']
const action = () => this.getXapiObject(vmId).$callAsync('start', false, false)
Expand Down

0 comments on commit 107de36

Please sign in to comment.