Skip to content

Commit 1b5af5a

Browse files
committed
feat: Use an ErrorHandler to generate JSON errors
1 parent 3772162 commit 1b5af5a

5 files changed

Lines changed: 68 additions & 122 deletions

File tree

packages/uma/config/default.json

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,25 @@
5858
"errorHandler": { "@id": "urn:uma:default:ErrorHandler" },
5959
"responseWriter": { "@id": "urn:uma:default:ResponseWriter" },
6060
"operationHandler": {
61-
"@id": "urn:uma:default:JsonHttpErrorHandler"
61+
"@id": "urn:uma:default:HttpHandlerController"
6262
}
6363
},
6464
{
65-
"@id": "urn:uma:default:JsonHttpErrorHandler",
66-
"@type": "JsonHttpErrorHandler",
67-
"nestedHandler": {
68-
"@id": "urn:uma:default:HttpHandlerController",
69-
"@type": "WaterfallHandler",
70-
"handlers": [
71-
{ "@id": "urn:uma:default:UmaConfigRouteHandler" },
72-
{ "@id": "urn:uma:default:JwksRouteHandler" },
73-
{ "@id": "urn:uma:default:TokenRouteHandler" },
74-
{ "@id": "urn:uma:default:PermissionRegistrationRouteHandler" },
75-
{ "@id": "urn:uma:default:ResourceRegistrationRouteHandler" },
76-
{ "@id": "urn:uma:default:ResourceRegistrationOpsRouteHandler" },
77-
{ "@id": "urn:uma:default:IntrospectionRouteHandler" },
78-
{
79-
"@type": "StaticThrowHandler",
80-
"error": { "@type": "NotFoundHttpError" }
81-
}
82-
]
83-
}
65+
"@id": "urn:uma:default:HttpHandlerController",
66+
"@type": "WaterfallHandler",
67+
"handlers": [
68+
{ "@id": "urn:uma:default:UmaConfigRouteHandler" },
69+
{ "@id": "urn:uma:default:JwksRouteHandler" },
70+
{ "@id": "urn:uma:default:TokenRouteHandler" },
71+
{ "@id": "urn:uma:default:PermissionRegistrationRouteHandler" },
72+
{ "@id": "urn:uma:default:ResourceRegistrationRouteHandler" },
73+
{ "@id": "urn:uma:default:ResourceRegistrationOpsRouteHandler" },
74+
{ "@id": "urn:uma:default:IntrospectionRouteHandler" },
75+
{
76+
"@type": "StaticThrowHandler",
77+
"error": { "@type": "NotFoundHttpError" }
78+
}
79+
]
8480
},
8581
{
8682
"comment": "Configuration for the UMA AS."
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
{
2-
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^7.0.0/components/context.jsonld",
2+
"@context": [
3+
"https://linkedsoftwaredependencies.org/bundles/npm/@solidlab/uma/^0.0.0/components/context.jsonld",
4+
"https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^7.0.0/components/context.jsonld"
5+
],
36
"@graph": [
47
{
5-
"comment": "Wraps around the main error handler as a fallback in case something goes wrong.",
68
"@id": "urn:uma:default:ErrorHandler",
7-
"@type": "SafeErrorHandler",
8-
"errorHandler": {
9-
"comment": "Redirects are created internally by throwing a specific error; this handler converts them to the correct response.",
10-
"@id": "urn:uma:default:RedirectingErrorHandler",
11-
"@type": "RedirectingErrorHandler"
12-
}
9+
"@type": "WaterfallHandler",
10+
"handlers": [
11+
{
12+
"comment": "Redirects are created internally by throwing a specific error; this handler converts them to the correct response.",
13+
"@id": "urn:uma:default:RedirectingErrorHandler",
14+
"@type": "RedirectingErrorHandler"
15+
},
16+
{
17+
"@id": "urn:uma:default:JsonErrorHandler",
18+
"@type": "JsonErrorHandler"
19+
}
20+
]
1321
}
1422
]
1523
}

packages/uma/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@ export * from './util/ReType';
5959

6060
// HTTP
6161
export * from './util/http/identifier/BaseTargetExtractor';
62-
export * from './util/http/server/ErrorHandler';
6362
export * from './util/http/server/InteractionRouterHandler';
63+
export * from './util/http/server/JsonErrorHandler';

packages/uma/src/util/http/server/ErrorHandler.ts

Lines changed: 0 additions & 93 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {
2+
APPLICATION_JSON,
3+
CONTENT_TYPE,
4+
ErrorHandler,
5+
ErrorHandlerArgs,
6+
getLoggerFor,
7+
guardedStreamFrom,
8+
RepresentationMetadata,
9+
ResponseDescription
10+
} from '@solid/community-server';
11+
12+
/**
13+
* {@link ErrorHandler} that returns a JSON representation of the error.
14+
*/
15+
export class JsonErrorHandler extends ErrorHandler {
16+
protected readonly logger = getLoggerFor(this);
17+
18+
public constructor(protected readonly showStackTrace = false) {
19+
super();
20+
}
21+
22+
public async handle({ request, error }: ErrorHandlerArgs): Promise<ResponseDescription> {
23+
this.logger.error(`Returned error for ${request.method} '${request.url}': ${error.name} ${error.message}`);
24+
25+
return new ResponseDescription(
26+
error.statusCode,
27+
new RepresentationMetadata({[CONTENT_TYPE]: APPLICATION_JSON}),
28+
guardedStreamFrom(JSON.stringify({
29+
status: error.statusCode,
30+
error: error.name,
31+
message: error.message,
32+
...(this.showStackTrace && error.stack ? { stack: error.stack } : {})
33+
})));
34+
}
35+
}

0 commit comments

Comments
 (0)