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.requests.validation.inheritedFromItem", message);
}

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,12 @@ 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()) {
const error = ConsumptionCoreErrors.requests.inheritedFromItem("Some child items have errors. Call 'canCreate' to get more information.");
if (canCreateResult.error.equals(error)) throw error;

throw canCreateResult.error;
}

const request = LocalRequest.from({
id: id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ 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."
code: "error.consumption.requests.validation.inheritedFromItem",
message: "Some child items have errors."
}
}
];
Expand Down Expand Up @@ -317,7 +317,7 @@ describe("DecideRequestParametersValidator", function () {
}

expect(validationResult).errorValidationResult({
code: "error.consumption.validation.inheritedFromItem"
code: "error.consumption.requests.validation.inheritedFromItem"
});

let childResult = validationResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,15 +364,15 @@ 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."
code: "error.consumption.requests.validation.inheritedFromItem",
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]).errorValidationResult({ code: "error.consumption.validation.inheritedFromItem" });
expect(validationResult.items[1]).errorValidationResult({ code: "error.consumption.requests.validation.inheritedFromItem" });

expect(validationResult.items[1].items).toHaveLength(3);
expect(validationResult.items[1].items[0].isError()).toBe(true);
Expand Down Expand Up @@ -558,15 +558,15 @@ describe("IncomingRequestsController", function () {
const validationResult = await When.iCallCanRejectWith(rejectParams);

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."
code: "error.consumption.requests.validation.inheritedFromItem",
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]).errorValidationResult({ code: "error.consumption.validation.inheritedFromItem" });
expect(validationResult.items[1]).errorValidationResult({ code: "error.consumption.requests.validation.inheritedFromItem" });

expect(validationResult.items[1].items).toHaveLength(3);
expect(validationResult.items[1].items[0].isError()).toBe(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ 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."
code: "error.consumption.requests.validation.inheritedFromItem",
message: "Some child items have errors."
});
expect(validationResult.items).toHaveLength(2);

Expand Down Expand Up @@ -184,18 +184,16 @@ 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."
code: "error.consumption.requests.validation.inheritedFromItem",
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.code).toBe("error.consumption.requests.validation.inheritedFromItem");
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,11 @@ 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. 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