From 0420eadea4e748c37326e951b27feb8634060d32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= <33655937+jkoenig134@users.noreply.github.com> Date: Tue, 5 Nov 2024 09:27:27 +0100 Subject: [PATCH] DataViewExpander is erroring for normal behavior of the RequestModule (#322) * feat: add RequestMessageErrorDVO * fix: return an error DVO instead of throwing and destoying the whole app * chore: better message * chore: update file --- .../runtime/src/dataViews/DataViewExpander.ts | 177 ++++++++---------- .../content/{MailDVOs.ts => MessageDVOs.ts} | 7 + .../runtime/src/dataViews/content/index.ts | 2 +- 3 files changed, 81 insertions(+), 105 deletions(-) rename packages/runtime/src/dataViews/content/{MailDVOs.ts => MessageDVOs.ts} (76%) diff --git a/packages/runtime/src/dataViews/DataViewExpander.ts b/packages/runtime/src/dataViews/DataViewExpander.ts index a037d89e0..02d8fd8a0 100644 --- a/packages/runtime/src/dataViews/DataViewExpander.ts +++ b/packages/runtime/src/dataViews/DataViewExpander.ts @@ -55,26 +55,7 @@ import { CoreAddress, CoreId } from "@nmshd/core-types"; import { IdentityController } from "@nmshd/transport"; import { Inject } from "@nmshd/typescript-ioc"; import _ from "lodash"; -import { - AuthenticationRequestItemDVO, - ConsentRequestItemDVO, - CreateAttributeRequestItemDVO, - DVOError, - DeleteAttributeRequestItemDVO, - FileDVO, - FreeTextRequestItemDVO, - IdentityDVO, - ProposeAttributeRequestItemDVO, - ReadAttributeRequestItemDVO, - RegisterAttributeListenerRequestItemDVO, - RelationshipTemplateDVO, - RequestItemDVO, - RequestItemGroupDVO, - ResponseDVO, - ShareAttributeRequestItemDVO -} from ".."; -import { TransportServices } from "../extensibility"; -import { ConsumptionServices } from "../extensibility/ConsumptionServices"; +import { ConsumptionServices, TransportServices } from "../extensibility"; import { FileDTO, IdentityDTO, @@ -92,7 +73,17 @@ import { import { RuntimeErrors } from "../useCases"; import { DataViewObject } from "./DataViewObject"; import { DataViewTranslateable } from "./DataViewTranslateable"; +import { DVOError } from "./common"; import { + DecidableAuthenticationRequestItemDVO, + DecidableConsentRequestItemDVO, + DecidableCreateAttributeRequestItemDVO, + DecidableDeleteAttributeRequestItemDVO, + DecidableFreeTextRequestItemDVO, + DecidableProposeAttributeRequestItemDVO, + DecidableReadAttributeRequestItemDVO, + DecidableRegisterAttributeListenerRequestItemDVO, + DecidableShareAttributeRequestItemDVO, LocalAttributeDVO, LocalAttributeListenerDVO, LocalRequestDVO, @@ -100,6 +91,7 @@ import { OwnRelationshipAttributeDVO, PeerAttributeDVO, PeerRelationshipAttributeDVO, + PeerRelationshipTemplateDVO, ProcessedAttributeQueryDVO, ProcessedIQLQueryDVO, ProcessedIdentityAttributeQueryDVO, @@ -109,46 +101,45 @@ import { RepositoryAttributeDVO, SharedToPeerAttributeDVO } from "./consumption"; -import { - DecidableAuthenticationRequestItemDVO, - DecidableConsentRequestItemDVO, - DecidableCreateAttributeRequestItemDVO, - DecidableDeleteAttributeRequestItemDVO, - DecidableFreeTextRequestItemDVO, - DecidableProposeAttributeRequestItemDVO, - DecidableReadAttributeRequestItemDVO, - DecidableRegisterAttributeListenerRequestItemDVO, - DecidableShareAttributeRequestItemDVO -} from "./consumption/DecidableRequestItemDVOs"; -import { PeerRelationshipTemplateDVO } from "./consumption/PeerRelationshipTemplateDVO"; -import { - AttributeQueryDVO, - DraftIdentityAttributeDVO, - DraftRelationshipAttributeDVO, - IQLQueryDVO, - IdentityAttributeQueryDVO, - RelationshipAttributeQueryDVO, - ThirdPartyRelationshipAttributeQueryDVO -} from "./content/AttributeDVOs"; -import { MailDVO, RequestMessageDVO } from "./content/MailDVOs"; -import { RequestDVO } from "./content/RequestDVO"; import { AttributeAlreadySharedAcceptResponseItemDVO, + AttributeQueryDVO, AttributeSuccessionAcceptResponseItemDVO, + AuthenticationRequestItemDVO, + ConsentRequestItemDVO, CreateAttributeAcceptResponseItemDVO, + CreateAttributeRequestItemDVO, DeleteAttributeAcceptResponseItemDVO, + DeleteAttributeRequestItemDVO, + DraftIdentityAttributeDVO, + DraftRelationshipAttributeDVO, ErrorResponseItemDVO, FreeTextAcceptResponseItemDVO, + FreeTextRequestItemDVO, + IQLQueryDVO, + IdentityAttributeQueryDVO, + MailDVO, ProposeAttributeAcceptResponseItemDVO, + ProposeAttributeRequestItemDVO, ReadAttributeAcceptResponseItemDVO, + ReadAttributeRequestItemDVO, RegisterAttributeListenerAcceptResponseItemDVO, + RegisterAttributeListenerRequestItemDVO, RejectResponseItemDVO, + RelationshipAttributeQueryDVO, + RequestDVO, + RequestItemDVO, + RequestItemGroupDVO, + RequestMessageDVO, + RequestMessageErrorDVO, + ResponseDVO, ResponseItemDVO, ResponseItemGroupDVO, - ShareAttributeAcceptResponseItemDVO -} from "./content/ResponseItemDVOs"; -import { MessageDVO, MessageStatus, RecipientDVO } from "./transport/MessageDVO"; -import { RelationshipDVO, RelationshipDirection } from "./transport/RelationshipDVO"; + ShareAttributeAcceptResponseItemDVO, + ShareAttributeRequestItemDVO, + ThirdPartyRelationshipAttributeQueryDVO +} from "./content"; +import { FileDVO, IdentityDVO, MessageDVO, MessageStatus, RecipientDVO, RelationshipDVO, RelationshipDirection, RelationshipTemplateDVO } from "./transport"; export class DataViewExpander { public constructor( @@ -235,7 +226,7 @@ export class DataViewExpander { } } - public async expandMessageDTO(message: MessageDTO | MessageWithAttachmentsDTO): Promise { + public async expandMessageDTO(message: MessageDTO | MessageWithAttachmentsDTO): Promise { const recipientRelationships = await this.expandRecipientDTOs(message.recipients); const addressMap: Record = {}; recipientRelationships.forEach((value) => (addressMap[value.id] = value)); @@ -314,31 +305,30 @@ export class DataViewExpander { } if (message.content["@type"] === "Request") { - let localRequest: LocalRequestDTO; - if (isOwn) { - const localRequestsResult = await this.consumption.outgoingRequests.getRequests({ - query: { "source.reference": message.id } - }); - if (localRequestsResult.value.length === 0) { - throw new Error("No LocalRequest has been found for this message id."); - } - if (localRequestsResult.value.length > 1) { - throw new Error("More than one LocalRequest has been found for this message id."); - } - localRequest = localRequestsResult.value[0]; - } else { - const localRequestsResult = await this.consumption.incomingRequests.getRequests({ - query: { "source.reference": message.id } - }); - if (localRequestsResult.value.length === 0) { - throw new Error("No LocalRequest has been found for this message id."); - } - if (localRequestsResult.value.length > 1) { - throw new Error("More than one LocalRequest has been found for this message id."); - } - localRequest = localRequestsResult.value[0]; + const query = { "source.reference": message.id }; + + const localRequestsResult = isOwn ? await this.consumption.outgoingRequests.getRequests({ query }) : await this.consumption.incomingRequests.getRequests({ query }); + + if (localRequestsResult.value.length === 0) { + return { + ...messageDVO, + type: "RequestMessageErrorDVO", + code: "dvo.requestMessage.error.noLocalRequest", + message: + "No LocalRequest has been found for this message id. This could be caused by an invalid Request in the Message content which could not be processed by the Request Module." + }; + } + + if (localRequestsResult.value.length > 1) { + return { + ...messageDVO, + type: "RequestMessageErrorDVO", + code: "dvo.requestMessage.error.multipleLocalRequests", + message: "More than one LocalRequest has been found for this message id." + }; } + const localRequest = localRequestsResult.value[0]; const requestMessageDVO: RequestMessageDVO = { ...messageDVO, type: "RequestMessageDVO", @@ -348,31 +338,13 @@ export class DataViewExpander { } if (message.content["@type"] === "ResponseWrapper") { - let localRequest: LocalRequestDTO; - if (isOwn) { - const localRequestsResult = await this.consumption.incomingRequests.getRequests({ - query: { id: message.content.requestId } - }); + const query = { id: message.content.requestId }; + const localRequestsResult = isOwn ? await this.consumption.outgoingRequests.getRequests({ query }) : await this.consumption.incomingRequests.getRequests({ query }); - if (localRequestsResult.value.length === 0) { - throw new Error("No LocalRequest has been found for this message id."); - } - if (localRequestsResult.value.length > 1) { - throw new Error("More than one LocalRequest has been found for this message id."); - } - localRequest = localRequestsResult.value[0]; - } else { - const localRequestsResult = await this.consumption.outgoingRequests.getRequests({ - query: { id: message.content.requestId } - }); - if (localRequestsResult.value.length === 0) { - throw new Error("No LocalRequest has been found for this message id."); - } - if (localRequestsResult.value.length > 1) { - throw new Error("More than one LocalRequest has been found for this message id."); - } - localRequest = localRequestsResult.value[0]; - } + if (localRequestsResult.value.length === 0) throw new Error("No LocalRequest has been found for this message id."); + if (localRequestsResult.value.length > 1) throw new Error("More than one LocalRequest has been found for this message id."); + + const localRequest = localRequestsResult.value[0]; const requestMessageDVO: RequestMessageDVO = { ...messageDVO, @@ -385,7 +357,7 @@ export class DataViewExpander { return messageDVO; } - public async expandMessageDTOs(messages: MessageDTO[]): Promise<(MessageDVO | MailDVO | RequestMessageDVO)[]> { + public async expandMessageDTOs(messages: MessageDTO[]): Promise<(MessageDVO | MailDVO | RequestMessageDVO | RequestMessageErrorDVO)[]> { const messagePromises = messages.map((message) => this.expandMessageDTO(message)); return await Promise.all(messagePromises); } @@ -1063,9 +1035,8 @@ export class DataViewExpander { ): Promise { const valueType = attribute.content.value["@type"]; const localAttribute = await this.consumptionController.attributes.getLocalAttribute(CoreId.from(attribute.id)); - if (!localAttribute) { - throw new Error("Attribute not found"); - } + if (!localAttribute) throw new Error("Attribute not found"); + const owner = attribute.content.owner; let name = `i18n://dvo.attribute.name.${valueType}`; @@ -1553,11 +1524,9 @@ export class DataViewExpander { public async expandAttribute(attribute: IdentityAttributeJSON | RelationshipAttributeJSON): Promise { const attributeInstance = Serializable.fromUnknown(attribute); - if (attributeInstance instanceof IdentityAttribute) { - return await this.expandIdentityAttribute(attribute as IdentityAttributeJSON, attributeInstance); - } else if (attributeInstance instanceof RelationshipAttribute) { - return await this.expandRelationshipAttribute(attribute as RelationshipAttributeJSON, attributeInstance); - } + if (attributeInstance instanceof IdentityAttribute) return await this.expandIdentityAttribute(attribute as IdentityAttributeJSON, attributeInstance); + if (attributeInstance instanceof RelationshipAttribute) return await this.expandRelationshipAttribute(attribute as RelationshipAttributeJSON, attributeInstance); + throw new Error("Wrong attribute instance"); } diff --git a/packages/runtime/src/dataViews/content/MailDVOs.ts b/packages/runtime/src/dataViews/content/MessageDVOs.ts similarity index 76% rename from packages/runtime/src/dataViews/content/MailDVOs.ts rename to packages/runtime/src/dataViews/content/MessageDVOs.ts index 0194371e4..d95eabe08 100644 --- a/packages/runtime/src/dataViews/content/MailDVOs.ts +++ b/packages/runtime/src/dataViews/content/MessageDVOs.ts @@ -7,6 +7,13 @@ export interface RequestMessageDVO extends Omit { request: LocalRequestDVO; } +export interface RequestMessageErrorDVO extends Omit { + type: "RequestMessageErrorDVO"; + + code: string; + message: string; +} + export interface MailDVO extends Omit { type: "MailDVO"; diff --git a/packages/runtime/src/dataViews/content/index.ts b/packages/runtime/src/dataViews/content/index.ts index 97080e91d..dd1a17ff7 100644 --- a/packages/runtime/src/dataViews/content/index.ts +++ b/packages/runtime/src/dataViews/content/index.ts @@ -1,5 +1,5 @@ export * from "./AttributeDVOs"; -export * from "./MailDVOs"; +export * from "./MessageDVOs"; export * from "./RequestDVO"; export * from "./RequestItemDVOs"; export * from "./ResponseDVO";