Skip to content

Commit 801847c

Browse files
authored
chore(errors): add custom error classes (#508)
* fixing errors from earlier pull request Signed-off-by: jriffs <[email protected]> * removed unwanted file �ersion.ts?? Signed-off-by: jriffs <[email protected]> * created more custom error classes and added test suites for them Signed-off-by: jriffs <[email protected]> --------- Signed-off-by: jriffs <[email protected]>
1 parent 0d16244 commit 801847c

File tree

11 files changed

+175
-23
lines changed

11 files changed

+175
-23
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 GRPCNotSupportedError extends Error {
15+
readonly name = "GRPCNotSupportedError";
16+
constructor() {
17+
super("GRPC is currently not supported.");
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 HTTPNotSupportedError extends Error {
15+
readonly name = "HTTPNotSupportedError";
16+
constructor() {
17+
super("HTTP is currently not supported.");
18+
}
19+
}
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/configuration.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { GetConfigurationResponse as GetConfigurationResponseResult } from "../.
1717
import HTTPClient from "./HTTPClient";
1818
import { SubscribeConfigurationCallback } from "../../../types/configuration/SubscribeConfigurationCallback";
1919
import { SubscribeConfigurationStream } from "../../../types/configuration/SubscribeConfigurationStream";
20+
import { HTTPNotSupportedError } from "../../../errors/HTTPNotSupportedError";
2021

2122
export default class HTTPClientConfiguration implements IClientConfiguration {
2223
client: HTTPClient;
@@ -26,15 +27,15 @@ export default class HTTPClientConfiguration implements IClientConfiguration {
2627
}
2728

2829
async subscribe(_storeName: string, _cb: SubscribeConfigurationCallback): Promise<SubscribeConfigurationStream> {
29-
throw new Error("HTTP is currently not supported.");
30+
throw new HTTPNotSupportedError();
3031
}
3132

3233
async subscribeWithKeys(
3334
_storeName: string,
3435
_keys: string[],
3536
_cb: SubscribeConfigurationCallback,
3637
): Promise<SubscribeConfigurationStream> {
37-
throw new Error("HTTP is currently not supported.");
38+
throw new HTTPNotSupportedError();
3839
}
3940

4041
async subscribeWithMetadata(
@@ -43,10 +44,10 @@ export default class HTTPClientConfiguration implements IClientConfiguration {
4344
_metadata: KeyValueType,
4445
_cb: SubscribeConfigurationCallback,
4546
): Promise<SubscribeConfigurationStream> {
46-
throw new Error("HTTP is currently not supported.");
47+
throw new HTTPNotSupportedError();
4748
}
4849

4950
async get(_storeName: string, _keys: string[], _metadata?: KeyValueType): Promise<GetConfigurationResponseResult> {
50-
throw new Error("HTTP is currently not supported.");
51+
throw new HTTPNotSupportedError();
5152
}
5253
}

src/implementation/Client/HTTPClient/proxy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import * as grpc from "@grpc/grpc-js";
1515
import Class from "../../../types/Class";
1616
import IClientProxy from "../../../interfaces/Client/IClientProxy";
1717
import HTTPClient from "./HTTPClient";
18+
import { HTTPNotSupportedError } from "../../../errors/HTTPNotSupportedError";
1819

1920
export default class HTTPClientProxy implements IClientProxy {
2021
client: HTTPClient;
@@ -24,6 +25,6 @@ export default class HTTPClientProxy implements IClientProxy {
2425
}
2526

2627
async create<T>(_cls: Class<T>, _clientOptions?: Partial<grpc.ClientOptions> | undefined): Promise<T> {
27-
throw new Error("HTTP is currently not supported.");
28+
throw new HTTPNotSupportedError();
2829
}
2930
}

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;

src/implementation/Server/GRPCServer/actor.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import GRPCServer from "./GRPCServer";
1515
import IServerActor from "../../../interfaces/Server/IServerActor";
1616
import AbstractActor from "../../../actors/runtime/AbstractActor";
1717
import Class from "../../../types/Class";
18+
import { GRPCNotSupportedError } from "../../../errors/GRPCNotSupportedError";
1819

1920
// https://docs.dapr.io/reference/api/bindings_api/
2021
export default class GRPCServerActor implements IServerActor {
@@ -25,18 +26,18 @@ export default class GRPCServerActor implements IServerActor {
2526
}
2627

2728
deactivateActor(_actorType: string, _actorId: string): Promise<void> {
28-
throw new Error("GRPC is currently not supported.");
29+
throw new GRPCNotSupportedError();
2930
}
3031

3132
init(): Promise<void> {
32-
throw new Error("GRPC is currently not supported.");
33+
throw new GRPCNotSupportedError();
3334
}
3435

3536
getRegisteredActors(): Promise<string[]> {
36-
throw new Error("GRPC is currently not supported.");
37+
throw new GRPCNotSupportedError();
3738
}
3839

3940
registerActor<T extends AbstractActor>(_cls: Class<T>): Promise<void> {
40-
throw new Error("GRPC is currently not supported.");
41+
throw new GRPCNotSupportedError();
4142
}
4243
}
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+
});

0 commit comments

Comments
 (0)