Skip to content

Commit

Permalink
Merge pull request #113 from sdkgen/fix/error-messages
Browse files Browse the repository at this point in the history
fix: error messages coming with an `Error:` prefix
  • Loading branch information
lbguilherme committed Oct 21, 2020
2 parents 1a953e7 + 3efe2fd commit e1e8bfd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
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

0 comments on commit e1e8bfd

Please sign in to comment.