Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the message of "error.consumption.validation.inheritedFromItem" when using canCreate #271

Merged
merged 9 commits into from
Sep 18, 2024
4 changes: 4 additions & 0 deletions packages/consumption/src/consumption/ConsumptionCoreErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ class Requests {
return new CoreError("error.consumption.requests.missingRelationship", message);
}

public inheritedFromItem(message: string) {
return new ApplicationError("error.consumption.validation.inheritedFromItem", message);
RuthDiG marked this conversation as resolved.
Show resolved Hide resolved
}

private static readonly _decideValidation = class {
public invalidNumberOfItems(message: string) {
return new ApplicationError("error.consumption.requests.decide.validation.invalidNumberOfItems", message);
Expand Down
9 changes: 2 additions & 7 deletions packages/consumption/src/modules/common/ValidationResult.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApplicationError } from "@js-soft/ts-utils";
import { ConsumptionCoreErrors } from "../../consumption/ConsumptionCoreErrors";

export abstract class ValidationResult {
protected constructor(public readonly items: ValidationResult[]) {}
Expand All @@ -21,13 +22,7 @@ export abstract class ValidationResult {

public static fromItems(items: ValidationResult[]): ValidationResult {
return items.some((r) => r.isError())
? ValidationResult.error(
new ApplicationError(
"error.consumption.validation.inheritedFromItem",
"Some child items have errors. If this error occurred during the specification of a Request, call 'canCreate' to get more information."
),
items
)
? ValidationResult.error(ConsumptionCoreErrors.requests.inheritedFromItem("Some child items have errors."), items)
: ValidationResult.success(items);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,15 @@ export class OutgoingRequestsController extends ConsumptionBaseController {
private async _create(id: CoreId, content: Request, peer: CoreAddress) {
const canCreateResult = await this.canCreate({ content, peer });

if (canCreateResult.isError()) throw canCreateResult.error;
if (canCreateResult.isError()) {
if (canCreateResult.error.code === "error.consumption.validation.inheritedFromItem") {
throw ConsumptionCoreErrors.requests.inheritedFromItem(
"Some child items have errors. If this error occurred during the specification of a Request, call 'canCreate' to get more information."
RuthDiG marked this conversation as resolved.
Show resolved Hide resolved
);
}
RuthDiG marked this conversation as resolved.
Show resolved Hide resolved

throw canCreateResult.error;
}

const request = LocalRequest.from({
id: id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ describe("DecideRequestParametersValidator", function () {
expectedError: {
indexPath: [0],
code: "error.consumption.validation.inheritedFromItem",
message: "Some child items have errors. If this error occurred during the specification of a Request, call 'canCreate' to get more information."
message: "Some child items have errors."
}
}
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ describe("IncomingRequestsController", function () {

expect(validationResult).errorValidationResult({
code: "error.consumption.validation.inheritedFromItem",
message: "Some child items have errors. If this error occurred during the specification of a Request, call 'canCreate' to get more information."
message: "Some child items have errors."
});
expect(validationResult.items).toHaveLength(2);

Expand Down Expand Up @@ -559,7 +559,7 @@ describe("IncomingRequestsController", function () {

expect(validationResult).errorValidationResult({
code: "error.consumption.validation.inheritedFromItem",
message: "Some child items have errors. If this error occurred during the specification of a Request, call 'canCreate' to get more information."
message: "Some child items have errors."
});
expect(validationResult.items).toHaveLength(2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe("OutgoingRequestsController", function () {
});
expect(validationResult).errorValidationResult({
code: "error.consumption.validation.inheritedFromItem",
message: "Some child items have errors. If this error occurred during the specification of a Request, call 'canCreate' to get more information."
message: "Some child items have errors."
});
expect(validationResult.items).toHaveLength(2);

Expand Down Expand Up @@ -185,17 +185,15 @@ describe("OutgoingRequestsController", function () {
});
expect(validationResult).errorValidationResult({
code: "error.consumption.validation.inheritedFromItem",
message: "Some child items have errors. If this error occurred during the specification of a Request, call 'canCreate' to get more information."
message: "Some child items have errors."
});
expect(validationResult.items).toHaveLength(2);

expect(validationResult.items[0].isError()).toBe(false);

expect(validationResult.items[1].isError()).toBe(true);
expect((validationResult.items[1] as ErrorValidationResult).error.code).toBe("error.consumption.validation.inheritedFromItem");
expect((validationResult.items[1] as ErrorValidationResult).error.message).toBe(
"Some child items have errors. If this error occurred during the specification of a Request, call 'canCreate' to get more information."
);
expect((validationResult.items[1] as ErrorValidationResult).error.message).toBe("Some child items have errors.");

expect(validationResult.items[1].items).toHaveLength(1);
expect(validationResult.items[1].items[0].isError()).toBe(true);
Expand Down Expand Up @@ -227,6 +225,13 @@ describe("OutgoingRequestsController", function () {
await Then.itThrowsAnErrorWithTheErrorMessage("*content*Value is not defined*");
});

test("throws that it is necessary to call 'canCreate' when at least one RequestItem is invalid", async function () {
await When.iTryToCreateAnOutgoingRequestWithIncorrectRequestItem();
await Then.itThrowsAnErrorWithTheErrorMessage(
"Some child items have errors. If this error occurred during the specification of a Request, call 'canCreate' to get more information."
);
});

test("throws when canCreate returns an error", async function () {
const oldCanCreate = context.outgoingRequestsController.canCreate;
context.outgoingRequestsController.canCreate = (_: ICreateOutgoingRequestParameters) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,30 @@ export class RequestsWhen {
return Promise.resolve();
}

public iTryToCreateAnOutgoingRequestWithIncorrectRequestItem(): Promise<void> {
const params: ICreateOutgoingRequestParameters = {
content: {
items: [
TestRequestItem.from({
mustBeAccepted: false,
shouldFailAtCanCreateOutgoingRequestItem: true
}),
TestRequestItem.from({
mustBeAccepted: false,
shouldFailAtCanCreateOutgoingRequestItem: true
})
]
},
peer: CoreAddress.from("did:e:a-domain:dids:anidentity")
};

this.context.actionToTry = async () => {
await this.context.outgoingRequestsController.create(params as any);
};

return Promise.resolve();
}

public iTryToCreateAnOutgoingRequestFromRelationshipTemplateResponseWithoutResponseSource(): Promise<void> {
const paramsWithoutResponseSource: Omit<ICreateAndCompleteOutgoingRequestFromRelationshipTemplateResponseParameters, "responseSource"> = {
response: TestObjectFactory.createResponse(),
Expand Down
31 changes: 31 additions & 0 deletions packages/runtime/test/modules/DeciderModule.test.ts
RuthDiG marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,35 @@ describe("DeciderModule", () => {
(e) => e.data.template.id === template.id && e.data.result === RelationshipTemplateProcessedResult.ManualRequestDecisionRequired
);
});

test("if you call canCreate for a request with an item which has an error you receive another errorMessage as if you call create", async () => {
const requestContent = {
content: {
items: [
{
"@type": "ShareAttributeRequestItem",
sourceAttributeId: "ATT",
attribute: {
"@type": "IdentityAttribute",
owner: (await sender.transport.account.getIdentityInfo()).value.address,
value: {
"@type": "GivenName",
value: "aGivenName"
}
},
mustBeAccepted: true
}
]
},
peer: (await recipient.transport.account.getIdentityInfo()).value.address
};
const canCreateResult = await sender.consumption.outgoingRequests.canCreate(requestContent);
const createResult = await sender.consumption.outgoingRequests.create(requestContent);
expect(createResult.error.code).toBe("error.consumption.validation.inheritedFromItem");
expect(createResult.error.message).toBe(
"Some child items have errors. If this error occurred during the specification of a Request, call 'canCreate' to get more information."
);
expect(canCreateResult.value.code).toBe("error.consumption.validation.inheritedFromItem");
expect(canCreateResult.value.message).toBe("Some child items have errors.");
});
});