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

fix: error messages coming with an Error: prefix #113

Merged
merged 1 commit into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion node-runtime/spec/rest/rest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ describe("Rest API", () => {
data: `{"val":0}`,
method: "POST",
path: "/obj",
result: `{"message":"Error: Value is zero ~ spec error","type":"Fatal"}`,
result: `{"message":"Value is zero ~ spec error","type":"Fatal"}`,
resultHeaders: {
"content-type": "application/json",
},
Expand Down
3 changes: 3 additions & 0 deletions node-runtime/spec/simple/api.sdkgen
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ type AllTypes {
date: date
datetime: datetime
}

error SomeError
fn throwsError(): void
11 changes: 11 additions & 0 deletions node-runtime/spec/simple/simple.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ api.fn.identity = async (ctx: Context & { aaa: boolean }, { types }: { types: an
return types;
};

api.fn.throwsError = async (ctx: Context) => {
throw api.err.SomeError("Some message");
};

// ExecSync(`../../cubos/sdkgen/sdkgen ${__dirname + "/api.sdkgen"} -o ${__dirname + "/legacyNodeClient.ts"} -t typescript_nodeclient`);
const { ApiClient: NodeLegacyApiClient } = require(`${__dirname}/legacyNodeClient.ts`);
const nodeLegacyClient = new NodeLegacyApiClient("http://localhost:8000");
Expand Down Expand Up @@ -99,4 +103,11 @@ describe("Simple API", () => {

expect(await nodeClient.identity(null, { types })).toEqual(types);
});

test("Errors are passed correctly", async () => {
await expect(nodeClient.throwsError(null, {})).rejects.toMatchObject({
message: "Some message",
type: "SomeError",
});
});
});
2 changes: 1 addition & 1 deletion node-runtime/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ export class SdkgenHttpServer<ExtraContextT = unknown> {

private makeResponseError(err: any): { message: string; type: string } {
return {
message: err.message || err instanceof Error ? err.toString() : typeof err === "object" ? JSON.stringify(err) : `${err}`,
message: err.message || (err instanceof Error ? err.toString() : typeof err === "object" ? JSON.stringify(err) : `${err}`),
type: err.type || "Fatal",
};
}
Expand Down