|
1 | 1 | import { describe, it } from "mocha"; |
2 | | -import { assert } from "chai"; |
| 2 | +import { assert, expect } from "chai"; |
3 | 3 | import * as sdk from "@hasura/ndc-sdk-typescript" |
4 | 4 | import { executeQuery } from "../../src/execution"; |
5 | 5 | import { FunctionNdcKind, FunctionsSchema } from "../../src/schema"; |
@@ -271,4 +271,97 @@ describe("execute query", function() { |
271 | 271 | ]); |
272 | 272 | assert.deepStrictEqual(functionCallCompletions, ["first", "third", "fourth", "second"]); |
273 | 273 | }); |
| 274 | + |
| 275 | + describe("function error handling", function() { |
| 276 | + const functionSchema: FunctionsSchema = { |
| 277 | + functions: { |
| 278 | + "theFunction": { |
| 279 | + ndcKind: FunctionNdcKind.Function, |
| 280 | + description: null, |
| 281 | + parallelDegree: null, |
| 282 | + arguments: [], |
| 283 | + resultType: { |
| 284 | + type: "named", |
| 285 | + kind: "scalar", |
| 286 | + name: "String" |
| 287 | + } |
| 288 | + } |
| 289 | + }, |
| 290 | + objectTypes: {}, |
| 291 | + scalarTypes: { |
| 292 | + "String": {} |
| 293 | + } |
| 294 | + }; |
| 295 | + const queryRequest: sdk.QueryRequest = { |
| 296 | + collection: "theFunction", |
| 297 | + query: { |
| 298 | + fields: {}, |
| 299 | + }, |
| 300 | + arguments: { |
| 301 | + "param": { |
| 302 | + type: "literal", |
| 303 | + value: "test" |
| 304 | + } |
| 305 | + }, |
| 306 | + collection_relationships: {} |
| 307 | + }; |
| 308 | + |
| 309 | + it("Error -> sdk.InternalServerError", async function() { |
| 310 | + const runtimeFunctions = { |
| 311 | + "theFunction": () => { |
| 312 | + throw new Error("BOOM!"); |
| 313 | + } |
| 314 | + }; |
| 315 | + |
| 316 | + await expect(executeQuery(queryRequest, functionSchema, runtimeFunctions)) |
| 317 | + .to.be.rejectedWith(sdk.InternalServerError, "Error encountered when invoking function 'theFunction'") |
| 318 | + .which.eventually.has.property("details") |
| 319 | + .which.include.keys("stack") |
| 320 | + .and.has.property("message", "BOOM!"); |
| 321 | + }); |
| 322 | + |
| 323 | + it("string -> sdk.InternalServerError", async function() { |
| 324 | + const runtimeFunctions = { |
| 325 | + "theFunction": () => { |
| 326 | + throw "A bad way to throw errors"; |
| 327 | + } |
| 328 | + }; |
| 329 | + |
| 330 | + await expect(executeQuery(queryRequest, functionSchema, runtimeFunctions)) |
| 331 | + .to.be.rejectedWith(sdk.InternalServerError, "Error encountered when invoking function 'theFunction'") |
| 332 | + .which.eventually.has.property("details") |
| 333 | + .and.has.property("message", "A bad way to throw errors"); |
| 334 | + }); |
| 335 | + |
| 336 | + it("unknown -> sdk.InternalServerError", async function() { |
| 337 | + const runtimeFunctions = { |
| 338 | + "theFunction": () => { |
| 339 | + throw 666; // What are you even doing? 👊 |
| 340 | + } |
| 341 | + }; |
| 342 | + |
| 343 | + await expect(executeQuery(queryRequest, functionSchema, runtimeFunctions)) |
| 344 | + .to.be.rejectedWith(sdk.InternalServerError, "Error encountered when invoking function 'theFunction'"); |
| 345 | + }); |
| 346 | + |
| 347 | + describe("sdk exceptions are passed through", function() { |
| 348 | + const exceptions = [ |
| 349 | + sdk.BadRequest, sdk.Forbidden, sdk.Conflict, sdk.UnprocessableContent, sdk.InternalServerError, sdk.NotSupported, sdk.BadGateway |
| 350 | + ]; |
| 351 | + |
| 352 | + for (const exceptionCtor of exceptions) { |
| 353 | + it(`sdk.${exceptionCtor.name}`, async function() { |
| 354 | + const runtimeFunctions = { |
| 355 | + "theFunction": () => { |
| 356 | + throw new exceptionCtor("Nope!", { deets: "stuff" }); |
| 357 | + } |
| 358 | + }; |
| 359 | + |
| 360 | + await expect(executeQuery(queryRequest, functionSchema, runtimeFunctions)) |
| 361 | + .to.be.rejectedWith(exceptionCtor, "Nope!") |
| 362 | + .and.eventually.property("details").deep.equals({"deets": "stuff"}); |
| 363 | + }); |
| 364 | + } |
| 365 | + }); |
| 366 | + }) |
274 | 367 | }); |
0 commit comments