Skip to content

Commit f365bbd

Browse files
committed
created more custom error classes and added test suites for them
Signed-off-by: jriffs <[email protected]>
1 parent c99ed17 commit f365bbd

File tree

6 files changed

+125
-14
lines changed

6 files changed

+125
-14
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
"test:e2e:common:client": "./scripts/test-e2e-common.sh client",
2121
"test:e2e:common:server": "./scripts/test-e2e-common.sh server",
2222
"test:unit": "jest --runInBand --detectOpenHandles",
23-
"test:unit:all": "npm run test:unit:main && npm run test:unit:http && npm run test:unit:grpc && npm run test:unit:actors && npm run test:unit:logger && npm run test:unit:utils",
23+
"test:unit:all": "npm run test:unit:main && npm run test:unit:http && npm run test:unit:grpc && npm run test:unit:actors && npm run test:unit:logger && npm run test:unit:utils && npm run test:unit:errors",
2424
"test:unit:main": "NODE_ENV=test npm run test:unit 'test/unit/main/.*\\.test\\.ts'",
2525
"test:unit:http": "NODE_ENV=test npm run test:unit 'test/unit/http/.*\\.test\\.ts'",
2626
"test:unit:grpc": "NODE_ENV=test npm run test:unit 'test/unit/grpc/.*\\.test\\.ts'",
2727
"test:unit:actors": "NODE_ENV=test npm run test:unit 'test/unit/actor/.*\\.test\\.ts'",
2828
"test:unit:logger": "NODE_ENV=test npm run test:unit 'test/unit/logger/.*\\.test\\.ts'",
2929
"test:unit:utils": "NODE_ENV=test npm run test:unit 'test/unit/utils/.*\\.test\\.ts'",
30+
"test:unit:errors": "NODE_ENV=test npm run test:unit 'test/unit/errors/.*\\.test\\.ts'",
3031
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
3132
"prebuild": "./scripts/prebuild.sh",
3233
"build-ci": "npm run prebuild && ./scripts/build.sh",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Copyright 2023 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
export class PropertyRequiredError extends Error {
15+
readonly name = "PropertyRequiredError";
16+
constructor(public propertyName: string) {
17+
super(`${propertyName} is required`);
18+
if (!propertyName || propertyName === " ") {
19+
throw new PropertyRequiredError("propertyName");
20+
}
21+
}
22+
}

src/implementation/Client/GRPCClient/workflow.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ limitations under the License.
1414
import GRPCClient from "./GRPCClient";
1515
import IClientWorkflow from "../../../interfaces/Client/IClientWorkflow";
1616
import { WorkflowGetResponseType } from "../../../types/workflow/WorkflowGetResponse.type";
17+
import { GRPCNotSupportedError } from "../../../errors/GRPCNotSupportedError";
1718

1819
export default class GRPCClientWorkflow implements IClientWorkflow {
1920
client: GRPCClient;
@@ -23,7 +24,7 @@ export default class GRPCClientWorkflow implements IClientWorkflow {
2324
}
2425

2526
get(_instanceId: string, _workflowComponent?: string | undefined): Promise<WorkflowGetResponseType> {
26-
throw new Error("Method not implemented.");
27+
throw new GRPCNotSupportedError();
2728
}
2829

2930
start(
@@ -32,26 +33,26 @@ export default class GRPCClientWorkflow implements IClientWorkflow {
3233
_instanceId?: string | undefined,
3334
_workflowComponent?: string | undefined,
3435
): Promise<string> {
35-
throw new Error("Method not implemented.");
36+
throw new GRPCNotSupportedError();
3637
}
3738

3839
terminate(_instanceId: string, _workflowComponent?: string | undefined): Promise<any> {
39-
throw new Error("Method not implemented.");
40+
throw new GRPCNotSupportedError();
4041
}
4142

4243
pause(_instanceId: string, _workflowComponent?: string | undefined): Promise<any> {
43-
throw new Error("Method not implemented.");
44+
throw new GRPCNotSupportedError();
4445
}
4546

4647
resume(_instanceId: string, _workflowComponent?: string | undefined): Promise<any> {
47-
throw new Error("Method not implemented.");
48+
throw new GRPCNotSupportedError();
4849
}
4950

5051
purge(_instanceId: string, _workflowComponent?: string | undefined): Promise<any> {
51-
throw new Error("Method not implemented.");
52+
throw new GRPCNotSupportedError();
5253
}
5354

5455
raise(_instanceId: string, _eventName: string, _input?: any, _workflowComponent?: string | undefined): Promise<any> {
55-
throw new Error("Method not implemented.");
56+
throw new GRPCNotSupportedError();
5657
}
5758
}

src/implementation/Client/HTTPClient/workflow.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { WorkflowStartOptions } from "../../../types/workflow/WorkflowStartOptio
2020
import { randomUUID } from "crypto";
2121
import { createHTTPQueryParam } from "../../../utils/Client.util";
2222
import { WorkflowRaiseOptions } from "../../../types/workflow/WorkflowRaiseOptions.type";
23+
import { PropertyRequiredError } from "../../../errors/PropertyRequiredError";
2324

2425
export default class HTTPClientWorkflow implements IClientWorkflow {
2526
private readonly client: HTTPClient;
@@ -34,7 +35,7 @@ export default class HTTPClientWorkflow implements IClientWorkflow {
3435

3536
async get(instanceID: string, workflowComponent?: string): Promise<WorkflowGetResponseType> {
3637
if (!instanceID) {
37-
throw new Error("instanceID is required");
38+
throw new PropertyRequiredError("instanceID");
3839
}
3940

4041
workflowComponent = workflowComponent ?? HTTPClientWorkflow.DEFAULT_WORKFLOW_COMPONENT;
@@ -76,7 +77,7 @@ export default class HTTPClientWorkflow implements IClientWorkflow {
7677
options: WorkflowStartOptions = {},
7778
): Promise<string> {
7879
if (!workflowName) {
79-
throw new Error("workflowName is required");
80+
throw new PropertyRequiredError("workflowName");
8081
}
8182

8283
if (!instanceId) {
@@ -120,11 +121,11 @@ export default class HTTPClientWorkflow implements IClientWorkflow {
120121
options: WorkflowRaiseOptions = {},
121122
): Promise<void> {
122123
if (!instanceId) {
123-
throw new Error("instanceID is required");
124+
throw new PropertyRequiredError("instanceID");
124125
}
125126

126127
if (!eventName) {
127-
throw new Error("eventName is required");
128+
throw new PropertyRequiredError("eventName");
128129
}
129130

130131
workflowComponent = workflowComponent ?? HTTPClientWorkflow.DEFAULT_WORKFLOW_COMPONENT;
@@ -170,11 +171,11 @@ export default class HTTPClientWorkflow implements IClientWorkflow {
170171

171172
async _invokeMethod(instanceId: string, method: string, workflowComponent?: string | undefined): Promise<any> {
172173
if (!instanceId) {
173-
throw new Error("instanceID is required");
174+
throw new PropertyRequiredError("instanceID");
174175
}
175176

176177
if (!method) {
177-
throw new Error("method is required");
178+
throw new PropertyRequiredError("method");
178179
}
179180

180181
workflowComponent = workflowComponent ?? HTTPClientWorkflow.DEFAULT_WORKFLOW_COMPONENT;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2023 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
import { PropertyRequiredError } from "../../../src/errors/PropertyRequiredError";
15+
16+
describe("PropertyRequiredError_class", () => {
17+
// Tests that the constructor creates an instance of PropertyRequiredError
18+
it("should be an instance of PropertyRequiredError", () => {
19+
const error = new PropertyRequiredError("test");
20+
expect(error).toBeInstanceOf(PropertyRequiredError);
21+
});
22+
23+
// Tests that the constructor sets the propertyName property correctly
24+
it("should set `propertyName` property correctly", () => {
25+
const propertyName = "test";
26+
const error = new PropertyRequiredError(propertyName);
27+
expect(error.propertyName).toBe(propertyName);
28+
});
29+
30+
// Tests that the constructor sets the message property correctly
31+
it("should set `message` property correctly", () => {
32+
const propertyName = "test";
33+
const error = new PropertyRequiredError(propertyName);
34+
expect(error.message).toBe(`${propertyName} is required`);
35+
});
36+
37+
// Tests that the constructor throws an error
38+
it("should throw an error if propertyName is an empty string", () => {
39+
expect(() => new PropertyRequiredError("")).toThrow();
40+
});
41+
});

test/unit/http/workflow.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2023 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
import HTTPClient from "../../../src/implementation/Client/HTTPClient/HTTPClient";
15+
import { PropertyRequiredError } from "../../../src/errors/PropertyRequiredError";
16+
import HTTPClientWorkflow from "../../../src/implementation/Client/HTTPClient/workflow";
17+
import { randomUUID } from "crypto";
18+
19+
describe("workflow", () => {
20+
const client = new HTTPClient({
21+
daprHost: "",
22+
daprPort: "",
23+
communicationProtocol: 0,
24+
});
25+
const workflow = new HTTPClientWorkflow(client);
26+
27+
it("should throw PropertyRequiredError when instanceID variable is not provided in get method", async () => {
28+
await expect(workflow.get("")).rejects.toThrow(PropertyRequiredError);
29+
});
30+
it("should throw PropertyRequiredError when workflowName variable is not provided in start method", async () => {
31+
await expect(workflow.start("")).rejects.toThrow(PropertyRequiredError);
32+
});
33+
it("should throw PropertyRequiredError when instanceID variable is not provided in _invokeMethod method", async () => {
34+
await expect(workflow._invokeMethod("", "raise")).rejects.toThrow(PropertyRequiredError);
35+
});
36+
it("should throw PropertyRequiredError when method variable is not provided in _invokeMethod method", async () => {
37+
await expect(workflow._invokeMethod(randomUUID(), "")).rejects.toThrow(PropertyRequiredError);
38+
});
39+
it("should throw PropertyRequiredError when instanceID variable is not provided in raise method", async () => {
40+
await expect(workflow.raise("", "Event")).rejects.toThrow(PropertyRequiredError);
41+
});
42+
it("should throw PropertyRequiredError when eventName variable is not provided in raise method", async () => {
43+
await expect(workflow.raise(randomUUID(), "")).rejects.toThrow(PropertyRequiredError);
44+
});
45+
});

0 commit comments

Comments
 (0)