Skip to content

Commit 35a82f1

Browse files
authored
Error shouldn't cause Client Fail If Actor Non-Existent method called (#422)
* Error shouldn't cause Client Fail If Actor Non-Existent method called Signed-off-by: Deepanshu Agarwal <[email protected]> * Add test Signed-off-by: Deepanshu Agarwal <[email protected]> * Fix pretty Signed-off-by: Deepanshu Agarwal <[email protected]> * Fix test Signed-off-by: Deepanshu Agarwal <[email protected]> * Fix test Signed-off-by: Deepanshu Agarwal <[email protected]> * Pretty fix Signed-off-by: Deepanshu Agarwal <[email protected]> * Incorporate review comments Signed-off-by: Deepanshu Agarwal <[email protected]> --------- Signed-off-by: Deepanshu Agarwal <[email protected]>
1 parent 96b63cb commit 35a82f1

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

src/implementation/Server/HTTPServer/actor.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import BufferSerializer from "../../../actors/runtime/BufferSerializer";
2222
import { DaprClient } from "../../..";
2323
import { Logger } from "../../../logger/Logger";
2424
import { getRegisteredActorResponse } from "../../../utils/Actors.util";
25+
import HttpStatusCode from "../../../enum/HttpStatusCode.enum";
2526

2627
// https://docs.dapr.io/reference/api/bindings_api/
2728
export default class HTTPServerActor implements IServerActor {
@@ -88,6 +89,7 @@ export default class HTTPServerActor implements IServerActor {
8889
private async handlerDeactivate(req: IRequest, res: IResponse): Promise<IResponse> {
8990
const { actorTypeName, actorId } = req.params;
9091
const result = await ActorRuntime.getInstance(this.client.getDaprClient()).deactivate(actorTypeName, actorId);
92+
res.statusCode = HttpStatusCode.OK;
9193
return this.handleResult(res, result);
9294
}
9395

@@ -98,13 +100,21 @@ export default class HTTPServerActor implements IServerActor {
98100
// @todo: reentrancy id? (https://github.com/dapr/python-sdk/blob/master/ext/flask_dapr/flask_dapr/actor.py#L91)
99101

100102
const dataSerialized = this.serializer.serialize(body);
101-
const result = await ActorRuntime.getInstance(this.client.getDaprClient()).invoke(
102-
actorTypeName,
103-
actorId,
104-
methodName,
105-
dataSerialized,
106-
);
107-
return this.handleResult(res, result);
103+
try {
104+
const result = await ActorRuntime.getInstance(this.client.getDaprClient()).invoke(
105+
actorTypeName,
106+
actorId,
107+
methodName,
108+
dataSerialized,
109+
);
110+
res.statusCode = HttpStatusCode.OK;
111+
return this.handleResult(res, result);
112+
} catch (err) {
113+
if (err instanceof Error) {
114+
res.statusCode = HttpStatusCode.INTERNAL_SERVER_ERROR;
115+
}
116+
return this.handleResult(res, err);
117+
}
108118
}
109119

110120
private async handlerTimer(req: IRequest, res: IResponse): Promise<IResponse> {
@@ -137,10 +147,9 @@ export default class HTTPServerActor implements IServerActor {
137147

138148
private handleResult(res: IResponse, result: any) {
139149
if (result && typeof result === "object") {
140-
return res.status(200).send(result);
150+
return res.status(res.statusCode).send(result);
141151
} else {
142-
// @ts-ignore
143-
return res.status(200).send(`${result}`);
152+
return res.status(res.statusCode).send(`${result}`);
144153
}
145154
}
146155
}

test/e2e/http/actors.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ describe("http/actors", () => {
133133
});
134134
});
135135

136+
describe("invokeNonExistentMethod", () => {
137+
it("should not fail if invoked non-existing method on actor", async () => {
138+
const builder = new ActorProxyBuilder<DemoActorCounterInterface>(DemoActorCounterImpl, client);
139+
const actorId = ActorId.createRandomId();
140+
builder.build(actorId);
141+
142+
const baseActorUrl = `http://${sidecarHost}:${sidecarPort}/v1.0/actors/DemoActorCounterImpl/${actorId.toString()}/method`;
143+
144+
const validFunc = await fetch(`${baseActorUrl}/getCounter`);
145+
expect(validFunc.status).toBe(200);
146+
expect(validFunc.statusText).toBe("OK");
147+
148+
const nonExistentFunc = await fetch(`${baseActorUrl}/sayHello`);
149+
expect(nonExistentFunc.status).toBe(500);
150+
expect(nonExistentFunc.statusText).toBe("Internal Server Error");
151+
});
152+
});
153+
136154
describe("invoke", () => {
137155
it("should register actors correctly", async () => {
138156
const actors = await server.actor.getRegisteredActors();

0 commit comments

Comments
 (0)