Skip to content

Commit

Permalink
Refactor: forward all exceptions to express exception handling middle…
Browse files Browse the repository at this point in the history
…ware
  • Loading branch information
sleelin committed Sep 14, 2024
1 parent 30f6531 commit 6ae11bf
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 35 deletions.
10 changes: 8 additions & 2 deletions src/routers.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ export class SCIMMYRouters extends Router {

next();
} else {
throw new TypeError("Method 'baseUri' must return a URL string in SCIMMYRouters constructor");
next(new TypeError("Method 'baseUri' must return a URL string in SCIMMYRouters constructor"));
}
} catch (ex) {
res.status(ex.status ?? 500).send(new SCIMMY.Messages.Error(ex));
next(ex);
}
});

Expand Down Expand Up @@ -178,6 +178,12 @@ export class SCIMMYRouters extends Router {

// If we get to this point, there's no matching endpoints
this.use((req, res) => res.status(404).send(new SCIMMY.Messages.Error({status: 404, message: "Endpoint Not Found"})));

// Handle any middleware exceptions, and if necessary, forward to next middleware
this.use((ex, req, res, next) => {
res.status(ex.status ?? 500).send(new SCIMMY.Messages.Error(ex));
if ((ex.status ?? 500) >= 500) next(ex);
});
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/routers/bulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ export class Bulk extends Router {
super({mergeParams: true});

// Respond to POST requests for /Bulk endpoint
this.post("/Bulk", async (req, res) => {
this.post("/Bulk", async (req, res, next) => {
try {
const {supported, maxPayloadSize, maxOperations} = SCIMMY.Config.get()?.bulk ?? {};

if (!supported) {
res.status(501).send(new SCIMMY.Messages.Error({status: 501, message: "Endpoint Not Implemented"}));
next(new SCIMMY.Types.Error(501, null, "Endpoint Not Implemented"));
} else if (Number(req.header("content-length")) > maxPayloadSize) {
throw new SCIMMY.Types.Error(413, null, `The size of the bulk operation exceeds maxPayloadSize limit (${maxPayloadSize})`);
next(new SCIMMY.Types.Error(413, null, `The size of the bulk operation exceeds maxPayloadSize limit (${maxPayloadSize})`));
} else {
res.status(200).send(await (new SCIMMY.Messages.BulkRequest(req.body, maxOperations)).apply(undefined, await context(req)));
}
} catch (ex) {
res.status(ex.status ?? 500).send(new SCIMMY.Messages.Error(ex));
next(ex);
}
});
}
Expand Down
10 changes: 5 additions & 5 deletions src/routers/me.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Me extends Router {
super({mergeParams: true});

// Respond to GET requests for /Me endpoint
this.get("/Me", async (req, res) => {
this.get("/Me", async (req, res, next) => {
try {
const id = await handler(req);
const isDeclared = SCIMMY.Resources.declared(SCIMMY.Resources.User);
Expand All @@ -25,15 +25,15 @@ export class Me extends Router {

// Set the actual location of the user resource, or respond with 501 not implemented
if (user && user?.meta?.location) res.location(user.meta.location).send(user);
else res.status(501).send(new SCIMMY.Messages.Error({status: 501, message: "Endpoint Not Implemented"}));
else next(new SCIMMY.Types.Error(501, null, "Endpoint Not Implemented"));
} catch (ex) {
res.status(ex.status ?? 500).send(new SCIMMY.Messages.Error(ex));
next(ex);
}
});

// Respond with 501 not implemented to all other requests for /Me endpoint
this.use("/Me", (req, res) => {
res.status(501).send(new SCIMMY.Messages.Error({status: 501, message: "Endpoint Not Implemented"}));
this.use("/Me", (req, res, next) => {
next(new SCIMMY.Types.Error(501, null, "Endpoint Not Implemented"));
});
}
}
24 changes: 12 additions & 12 deletions src/routers/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,52 @@ export class Resources extends Router {
// Mount /.search endpoint for resource
this.use(new Search(Resource, context));

this.get("/", async (req, res) => {
this.get("/", async (req, res, next) => {
try {
res.send(await new Resource(req.query).read(await context(req)));
} catch (ex) {
res.status(ex.status ?? 500).send(new SCIMMY.Messages.Error(ex));
next(ex);
}
});

this.get("/:id", async (req, res) => {
this.get("/:id", async (req, res, next) => {
try {
res.send(await new Resource(req.params.id, req.query).read(await context(req)));
} catch (ex) {
res.status(ex.status ?? 500).send(new SCIMMY.Messages.Error(ex));
next(ex);
}
});

this.post("/", async (req, res) => {
this.post("/", async (req, res, next) => {
try {
res.status(201).send(await new Resource(req.query).write(req.body, await context(req)));
} catch (ex) {
res.status(ex.status ?? 500).send(new SCIMMY.Messages.Error(ex));
next(ex);
}
});

this.put("/:id", async (req, res) => {
this.put("/:id", async (req, res, next) => {
try {
res.send(await new Resource(req.params.id, req.query).write(req.body, await context(req)));
} catch (ex) {
res.status(ex.status ?? 500).send(new SCIMMY.Messages.Error(ex));
next(ex);
}
});

this.patch("/:id", async (req, res) => {
this.patch("/:id", async (req, res, next) => {
try {
const value = await new Resource(req.params.id, req.query).patch(req.body, await context(req));
res.status(!!value ? 200 : 204).send(value);
} catch (ex) {
res.status(ex.status ?? 500).send(new SCIMMY.Messages.Error(ex));
next(ex);
}
});

this.delete("/:id", async (req, res) => {
this.delete("/:id", async (req, res, next) => {
try {
res.status(204).send(await new Resource(req.params.id).dispose(await context(req)));
} catch (ex) {
res.status(ex.status ?? 500).send(new SCIMMY.Messages.Error(ex));
next(ex);
}
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/routers/resourcetypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ export class ResourceTypes extends Router {
constructor() {
super({mergeParams: true});

this.get("/ResourceTypes", async (req, res) => {
this.get("/ResourceTypes", async (req, res, next) => {
try {
res.send(await new SCIMMY.Resources.ResourceType(req.query).read());
} catch (ex) {
res.status(ex.status ?? 500).send(new SCIMMY.Messages.Error(ex));
next(ex);
}
});

this.get("/ResourceTypes/:id", async (req, res) => {
this.get("/ResourceTypes/:id", async (req, res, next) => {
try {
res.send(await new SCIMMY.Resources.ResourceType(req.params.id, req.query).read());
} catch (ex) {
res.status(ex.status ?? 500).send(new SCIMMY.Messages.Error(ex));
next(ex);
}
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/routers/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ export class Schemas extends Router {
constructor() {
super({mergeParams: true});

this.get("/Schemas", async (req, res) => {
this.get("/Schemas", async (req, res, next) => {
try {
res.send(await new SCIMMY.Resources.Schema(req.query).read());
} catch (ex) {
res.status(ex.status ?? 500).send(new SCIMMY.Messages.Error(ex));
next(ex);
}
});

this.get("/Schemas/:id", async (req, res) => {
this.get("/Schemas/:id", async (req, res, next) => {
try {
res.send(await new SCIMMY.Resources.Schema(req.params.id, req.query).read());
} catch (ex) {
res.status(ex.status ?? 500).send(new SCIMMY.Messages.Error(ex));
next(ex);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/routers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export class Search extends Router {
super({mergeParams: true});

// Respond to POST requests for /.search endpoint
this.post("/.search", async (req, res) => {
this.post("/.search", async (req, res, next) => {
try {
res.status(200).send(await (new SCIMMY.Messages.SearchRequest(req.body)).apply(Resource.prototype instanceof SCIMMY.Types.Resource ? [Resource] : undefined, await context(req)));
} catch (ex) {
res.status(ex.status ?? 500).send(new SCIMMY.Messages.Error(ex));
next(ex);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/routers/spconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export class ServiceProviderConfig extends Router {
constructor() {
super({mergeParams: true});

this.get("/ServiceProviderConfig", async (req, res) => {
this.get("/ServiceProviderConfig", async (req, res, next) => {
try {
res.send(await new SCIMMY.Resources.ServiceProviderConfig(req.query).read());
} catch (ex) {
res.status(ex.status ?? 500).send(new SCIMMY.Messages.Error(ex));
next(ex);
}
});
}
Expand Down

0 comments on commit 6ae11bf

Please sign in to comment.