mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Create ErrorHandler to convert errors to Representations
This commit is contained in:
@@ -26,7 +26,7 @@ import {
|
||||
} from '../util/PathUtil';
|
||||
import { parseQuads } from '../util/QuadUtil';
|
||||
import { addResourceMetadata } from '../util/ResourceUtil';
|
||||
import { CONTENT_TYPE, DC, HTTP, LDP, POSIX, PIM, RDF, VANN, XSD } from '../util/Vocabularies';
|
||||
import { CONTENT_TYPE, DC, SOLID_HTTP, LDP, POSIX, PIM, RDF, VANN, XSD } from '../util/Vocabularies';
|
||||
import type { DataAccessor } from './accessors/DataAccessor';
|
||||
import type { ResourceStore } from './ResourceStore';
|
||||
|
||||
@@ -407,8 +407,8 @@ export class DataAccessorBasedStore implements ResourceStore {
|
||||
Promise<ResourceIdentifier> {
|
||||
// Get all values needed for naming the resource
|
||||
const isContainer = this.isNewContainer(metadata);
|
||||
const slug = metadata.get(HTTP.slug)?.value;
|
||||
metadata.removeAll(HTTP.slug);
|
||||
const slug = metadata.get(SOLID_HTTP.slug)?.value;
|
||||
metadata.removeAll(SOLID_HTTP.slug);
|
||||
|
||||
let newID: ResourceIdentifier = this.createURI(container, isContainer, slug);
|
||||
|
||||
@@ -439,7 +439,7 @@ export class DataAccessorBasedStore implements ResourceStore {
|
||||
if (this.hasContainerType(metadata.getAll(RDF.type))) {
|
||||
return true;
|
||||
}
|
||||
const slug = suffix ?? metadata.get(HTTP.slug)?.value;
|
||||
const slug = suffix ?? metadata.get(SOLID_HTTP.slug)?.value;
|
||||
return Boolean(slug && isContainerPath(slug));
|
||||
}
|
||||
|
||||
|
||||
37
src/storage/conversion/ErrorToQuadConverter.ts
Normal file
37
src/storage/conversion/ErrorToQuadConverter.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import arrayifyStream from 'arrayify-stream';
|
||||
import { BasicRepresentation } from '../../ldp/representation/BasicRepresentation';
|
||||
import type { Representation } from '../../ldp/representation/Representation';
|
||||
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
|
||||
import { INTERNAL_ERROR, INTERNAL_QUADS } from '../../util/ContentTypes';
|
||||
import { InternalServerError } from '../../util/errors/InternalServerError';
|
||||
import { DC, SOLID_ERROR } from '../../util/Vocabularies';
|
||||
import type { RepresentationConverterArgs } from './RepresentationConverter';
|
||||
import { TypedRepresentationConverter } from './TypedRepresentationConverter';
|
||||
|
||||
/**
|
||||
* Converts an error object into quads by creating a triple for each of name/message/stack.
|
||||
*/
|
||||
export class ErrorToQuadConverter extends TypedRepresentationConverter {
|
||||
public constructor() {
|
||||
super(INTERNAL_ERROR, INTERNAL_QUADS);
|
||||
}
|
||||
|
||||
public async handle({ identifier, representation }: RepresentationConverterArgs): Promise<Representation> {
|
||||
const errors = await arrayifyStream(representation.data);
|
||||
if (errors.length !== 1) {
|
||||
throw new InternalServerError('Only single errors are supported.');
|
||||
}
|
||||
const error = errors[0] as Error;
|
||||
|
||||
// A metadata object makes it easier to add triples due to the utility functions
|
||||
const data = new RepresentationMetadata(identifier);
|
||||
data.add(DC.terms.title, error.name);
|
||||
data.add(DC.terms.description, error.message);
|
||||
if (error.stack) {
|
||||
data.add(SOLID_ERROR.terms.stack, error.stack);
|
||||
}
|
||||
|
||||
// Update the content-type to quads
|
||||
return new BasicRepresentation(data.quads(), representation.metadata, INTERNAL_QUADS, false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user