Skip to content

Commit

Permalink
DataViewExpander is erroring for normal behavior of the RequestModule (
Browse files Browse the repository at this point in the history
…#322)

* feat: add RequestMessageErrorDVO

* fix: return an error DVO instead of throwing and destoying the whole app

* chore: better message

* chore: update file
  • Loading branch information
jkoenig134 authored Nov 5, 2024
1 parent 8b6f0e6 commit 0420ead
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 105 deletions.
177 changes: 73 additions & 104 deletions packages/runtime/src/dataViews/DataViewExpander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -92,14 +73,25 @@ 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,
LocalResponseDVO,
OwnRelationshipAttributeDVO,
PeerAttributeDVO,
PeerRelationshipAttributeDVO,
PeerRelationshipTemplateDVO,
ProcessedAttributeQueryDVO,
ProcessedIQLQueryDVO,
ProcessedIdentityAttributeQueryDVO,
Expand All @@ -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(
Expand Down Expand Up @@ -235,7 +226,7 @@ export class DataViewExpander {
}
}

public async expandMessageDTO(message: MessageDTO | MessageWithAttachmentsDTO): Promise<MessageDVO | MailDVO | RequestMessageDVO> {
public async expandMessageDTO(message: MessageDTO | MessageWithAttachmentsDTO): Promise<MessageDVO | MailDVO | RequestMessageDVO | RequestMessageErrorDVO> {
const recipientRelationships = await this.expandRecipientDTOs(message.recipients);
const addressMap: Record<string, RecipientDVO> = {};
recipientRelationships.forEach((value) => (addressMap[value.id] = value));
Expand Down Expand Up @@ -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",
Expand All @@ -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,
Expand All @@ -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);
}
Expand Down Expand Up @@ -1063,9 +1035,8 @@ export class DataViewExpander {
): Promise<RepositoryAttributeDVO | SharedToPeerAttributeDVO | PeerAttributeDVO | PeerRelationshipAttributeDVO | OwnRelationshipAttributeDVO> {
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}`;
Expand Down Expand Up @@ -1553,11 +1524,9 @@ export class DataViewExpander {

public async expandAttribute(attribute: IdentityAttributeJSON | RelationshipAttributeJSON): Promise<DraftIdentityAttributeDVO | DraftRelationshipAttributeDVO> {
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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ export interface RequestMessageDVO extends Omit<MessageDVO, "type"> {
request: LocalRequestDVO;
}

export interface RequestMessageErrorDVO extends Omit<MessageDVO, "type"> {
type: "RequestMessageErrorDVO";

code: string;
message: string;
}

export interface MailDVO extends Omit<MessageDVO, "type"> {
type: "MailDVO";

Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/dataViews/content/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "./AttributeDVOs";
export * from "./MailDVOs";
export * from "./MessageDVOs";
export * from "./RequestDVO";
export * from "./RequestItemDVOs";
export * from "./ResponseDVO";
Expand Down

0 comments on commit 0420ead

Please sign in to comment.