Skip to content

Commit 3ad1fa5

Browse files
Sharing a request with yourself shows a misleading error message (#280)
* feat: improve error message when sharing a request with yourself * chore: pr changes * chore: pr changes * chore: fix prettier * test: add test for new error item * chore: correct error code --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 390ea01 commit 3ad1fa5

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

packages/consumption/src/consumption/ConsumptionCoreErrors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ class Requests {
310310
return new ApplicationError("error.consumption.requests.validation.inheritedFromItem", message);
311311
}
312312

313+
public cannotShareRequestWithYourself() {
314+
return new CoreError("error.consumption.requests.cannotShareRequestWithYourself", "You cannot share a Request with yourself.");
315+
}
316+
313317
private static readonly _decideValidation = class {
314318
public invalidNumberOfItems(message: string) {
315319
return new ApplicationError("error.consumption.requests.decide.validation.invalidNumberOfItems", message);

packages/consumption/src/modules/requests/outgoing/OutgoingRequestsController.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ export class OutgoingRequestsController extends ConsumptionBaseController {
4040

4141
public async canCreate(params: ICanCreateOutgoingRequestParameters): Promise<ValidationResult> {
4242
const parsedParams = CanCreateOutgoingRequestParameters.from(params);
43+
44+
if (parsedParams.peer?.equals(this.identity.address)) {
45+
return ValidationResult.error(ConsumptionCoreErrors.requests.cannotShareRequestWithYourself());
46+
}
47+
4348
if (parsedParams.peer) {
4449
const relationship = await this.relationshipResolver.getRelationshipToIdentity(parsedParams.peer);
4550

packages/consumption/test/modules/requests/OutgoingRequestsController.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,32 @@ describe("OutgoingRequestsController", function () {
198198
expect(validationResult.items[1].items).toHaveLength(1);
199199
expect(validationResult.items[1].items[0].isError()).toBe(true);
200200
});
201+
202+
test("returns a validation result that contains an error for requests to myself", async function () {
203+
const validationResult = await When.iCallCanCreateForAnOutgoingRequest({
204+
content: {
205+
items: [
206+
TestRequestItem.from({
207+
mustBeAccepted: false
208+
}),
209+
RequestItemGroup.from({
210+
items: [
211+
TestRequestItem.from({
212+
mustBeAccepted: false,
213+
shouldFailAtCanCreateOutgoingRequestItem: true
214+
})
215+
]
216+
})
217+
]
218+
},
219+
peer: context.currentIdentity.address
220+
});
221+
222+
expect(validationResult).errorValidationResult({
223+
code: "error.consumption.requests.cannotShareRequestWithYourself",
224+
message: "You cannot share a Request with yourself."
225+
});
226+
});
201227
});
202228

203229
describe("Create (on active relationship)", function () {

0 commit comments

Comments
 (0)