feat: Improve path logging.

This commit is contained in:
Ruben Verborgh 2021-01-01 19:00:14 +01:00
parent c41c41d0e9
commit e20510a392
4 changed files with 10 additions and 9 deletions

View File

@ -64,6 +64,7 @@ export class RootContainerInitializer extends Initializer {
metadata.contentType = TEXT_TURTLE; metadata.contentType = TEXT_TURTLE;
this.logger.debug(`Creating root container at ${this.baseId.path}`);
await this.store.setRepresentation(this.baseId, { await this.store.setRepresentation(this.baseId, {
binary: true, binary: true,
data: guardedStreamFrom([]), data: guardedStreamFrom([]),

View File

@ -64,8 +64,8 @@ export class ExtensionBasedMapper implements FileIdentifierMapper {
* @returns A ResourceLink with all the necessary metadata. * @returns A ResourceLink with all the necessary metadata.
*/ */
public async mapUrlToFilePath(identifier: ResourceIdentifier, contentType?: string): Promise<ResourceLink> { public async mapUrlToFilePath(identifier: ResourceIdentifier, contentType?: string): Promise<ResourceLink> {
const path = getRelativePath(this.baseRequestURI, identifier, this.logger); const path = getRelativePath(this.baseRequestURI, identifier);
validateRelativePath(path, identifier, this.logger); validateRelativePath(path, identifier);
let filePath = getAbsolutePath(this.rootFilepath, path); let filePath = getAbsolutePath(this.rootFilepath, path);

View File

@ -29,8 +29,8 @@ export class FixedContentTypeMapper implements FileIdentifierMapper {
} }
public async mapUrlToFilePath(identifier: ResourceIdentifier, contentType?: string): Promise<ResourceLink> { public async mapUrlToFilePath(identifier: ResourceIdentifier, contentType?: string): Promise<ResourceLink> {
const path = getRelativePath(this.baseRequestURI, identifier, this.logger); const path = getRelativePath(this.baseRequestURI, identifier);
validateRelativePath(path, identifier, this.logger); validateRelativePath(path, identifier);
const filePath = getAbsolutePath(this.rootFilepath, path); const filePath = getAbsolutePath(this.rootFilepath, path);

View File

@ -1,12 +1,14 @@
import { posix } from 'path'; import { posix } from 'path';
import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier'; import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
import type { Logger } from '../../logging/Logger'; import { getLoggerFor } from '../../logging/LogUtil';
import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError'; import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError';
import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError'; import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError';
import { decodeUriPathComponents } from '../../util/PathUtil'; import { decodeUriPathComponents } from '../../util/PathUtil';
const { join: joinPath } = posix; const { join: joinPath } = posix;
const logger = getLoggerFor('MapperUtil');
/** /**
* Get the absolute file path based on the rootFilepath of the store. * Get the absolute file path based on the rootFilepath of the store.
* @param rootFilepath - The root file path. * @param rootFilepath - The root file path.
@ -22,14 +24,13 @@ export const getAbsolutePath = (rootFilepath: string, path: string, identifier =
* Strips the baseRequestURI from the identifier and checks if the stripped base URI matches the store's one. * Strips the baseRequestURI from the identifier and checks if the stripped base URI matches the store's one.
* @param baseRequestURI - Base URL for requests. * @param baseRequestURI - Base URL for requests.
* @param identifier - Incoming identifier. * @param identifier - Incoming identifier.
* @param logger - A logger instance.
* *
* @throws {@link NotFoundHttpError} * @throws {@link NotFoundHttpError}
* If the identifier does not match the baseRequestURI path of the store. * If the identifier does not match the baseRequestURI path of the store.
* *
* @returns A string representing the relative path. * @returns A string representing the relative path.
*/ */
export const getRelativePath = (baseRequestURI: string, identifier: ResourceIdentifier, logger: Logger): string => { export const getRelativePath = (baseRequestURI: string, identifier: ResourceIdentifier): string => {
if (!identifier.path.startsWith(baseRequestURI)) { if (!identifier.path.startsWith(baseRequestURI)) {
logger.warn(`The URL ${identifier.path} is outside of the scope ${baseRequestURI}`); logger.warn(`The URL ${identifier.path} is outside of the scope ${baseRequestURI}`);
throw new NotFoundHttpError(); throw new NotFoundHttpError();
@ -45,9 +46,8 @@ export const getRelativePath = (baseRequestURI: string, identifier: ResourceIden
* *
* @param path - A relative path, as generated by {@link getRelativePath}. * @param path - A relative path, as generated by {@link getRelativePath}.
* @param identifier - A resource identifier. * @param identifier - A resource identifier.
* @param logger - A logger instance.
*/ */
export const validateRelativePath = (path: string, identifier: ResourceIdentifier, logger: Logger): void => { export const validateRelativePath = (path: string, identifier: ResourceIdentifier): void => {
if (!path.startsWith('/')) { if (!path.startsWith('/')) {
logger.warn(`URL ${identifier.path} needs a / after the base`); logger.warn(`URL ${identifier.path} needs a / after the base`);
throw new BadRequestHttpError('URL needs a / after the base'); throw new BadRequestHttpError('URL needs a / after the base');