Troubleshooting


This page contains some troubleshooting techniques and FAQs for CDK

Error handling

You could use withErrorHandling() on the response to display custom error messages to the users.

Copy
Copied
handler.usingHttp((http) =>
	http
	.get('<url>')
	.handleRequest((ctx, input, request) =>
		request.withBearerToken(ctx.auth!.user.access_token).withoutBody()
	)
	.handleResponse((ctx, input, response) =>
		response
		.withErrorHandling(() => {
			const status = response.getStatusCode();
			if (status === 404) {
				return OperationHandlerResult.failure(
					OperationHandlerError.userInputError('post not found')
				);
			}
			return OperationHandlerResult.failure(
				OperationHandlerError.apiError(`api error: ${status}`)
			);
		})
		.parseWithBodyAsJson()
	)
);

JSON Error Handling

You can decode a error body that uses the JSON content type and access the body inside of an error handling function.

This way you can enrich the errors that can are surfaced to the user.

Copy
Copied
handler.withGlobalConfiguration(globalConfigHttp).usingHttp((http) =>
  http
    .get("/user/repos")
    .handleRequest((ctx, input, request) => request.withoutBody())
    .handleResponse(
      (ctx, input, response) =>
        response.withJsonErrorHandling <
        {
          message: string,
        } >
        ((body) =>
          OperationHandlerResult.failure(
            OperationHandlerError.apiError(
              `API returned an error: ${
                body.message
              }, status code: ${response.getStatusCode()}`,
              {
                statusCode: response.getStatusCode(),
              }
            )
          )).parseWithBodyAsJson()
    )
);

Logging requests and responses

You can use console.log statement in the handler to log requests and responses:

Copy
Copied
export const getTopRatedMoviesHandler = OperationHandlerSetup.configureHandler<
  TmdbAuth,
  GetTopRatedMoviesInput,
  GetTopRatedMoviesOutput
>((handler) =>
  handler.withGlobalConfiguration(globalConfigHttp).usingHttp((http) =>
    http
      .get("/3/movie/top_rated")
      .handleRequest((_ctx, _input, request) => {
        console.log(request.withoutBody());
        return request.withoutBody();
      })
      .handleResponse((_ctx, _input, response) => {
        console.log(response.parseWithBodyAsJson());
        return response.parseWithBodyAsJson();
      })
  )
);

You can also log responses in the handler.test files:

Copy
Copied
OperationHandlerTestSetup.configureHandlerTest(
  getTopRatedMoviesHandler,
  (handlerTest) =>
    handlerTest
      .usingHandlerContext("test")
      .nothingBeforeAll()
      .testCase("should return 20 movies", (testCase) =>
        testCase
          .givenNothing()
          .when(() => ({}))
          .then(({ output }) => {
            const outputValue =
              OperationHandlerResult.getSuccessfulValueOrFail(output);
            console.log(outputValue);
            expect(outputValue.results.length).toEqual(20);
          })
          .finallyDoNothing()
      )
      .nothingAfterAll()
);

Request Timeouts

Your tests may time out if the 3rd party service API is taking longer than 5 seconds to respond.

This is because the testing library for CDK: jest has a default timeout of 5 seconds for each test.

To increase timeout for a particular operation, simply add jest.setTimeout(<timeout value in milliseconds>) to your handler.test.ts file. e.g.

Copy
Copied
jest.setTimeout(30000); //30000 milliseconds

You can increase timeout for all of your tests by setting testTimeout property in jest.config.json file. e.g. "testTimeout": 30000 where 30000 is the timeout in milliseconds.

Deployment errors

You may encounter the below error while deploying the connector:

Copy
Copied
Error (500): {"message":"ENOTEMPTY: directory not empty, rmdir './tmp/connectors/cloudscale-Dubber/node_modules/@types/node'","\_tag":"InternalError"}

To solve this, simpley delete node_modules folder and perform a reinstall using npm i