calcom / cal.com

Scheduling infrastructure for absolutely everyone.
https://cal.com
Other
30.2k stars 7.17k forks source link

[CAL-3734] [CAL-3735] Get bookings via API returns 500 error #14735

Closed pgvr closed 1 week ago

pgvr commented 3 months ago

Found a bug? Please fill out the sections below. 👍

Issue Summary

Get bookings via API returns 500 error. This happens for a certain API key, that I can share with someone from Cal.com, if need be. For new keys that I create this request works.

Actual Results

Expected Results

Evidence

image

From SyncLinear.com | CAL-3734

dosubot[bot] commented 3 months ago

Hey there, @pgvr! 👋 Fancy seeing you here again - hope this message finds you less bugged than your API key seems to be. 😅 I'm diving into your issue as we speak, and I'll circle back with a diagnostic spell shortly. 🧙‍♂️

sumitdashrathgupta commented 3 months ago

1) Verify API Key: Ensure that the API key you're using for the requests causing the 500 error is correct and active. Check for any typos or discrepancies in the key. 2) Check API Documentation: Review the documentation for the API endpoint you're trying to access. Make sure you're forming the request correctly, including any required headers or parameters. 3) Review Error Logs: If possible, access error logs from the API server to see if there's more specific information about why the request is failing. This can provide insights into the root cause of the issue. 4) Compare Requests: Compare the requests made with the working API keys to the one that's causing the error. Look for any differences in headers, parameters, or payload that could be contributing to the problem. 5) Test in Isolation: If you have a sandbox or test environment for the API, try making the problematic request there. This can help determine if the issue is specific to the production environment or if it's related to the API key itself. 6) Contact Support: If you're still unable to resolve the issue, reach out to the support team for the API provider. Provide them with details about the problem, including the specific endpoint, request payload, and any error messages received. If necessary, share the API key causing the problem with them for further investigation. 7) Check for Rate Limits or Restrictions: Ensure that the API key causing the issue isn't subject to any rate limits or access restrictions that could be causing the 500 error. Review the API provider's documentation for any relevant information in this regard.

dosubot[bot] commented 3 months ago

To address the issue with the Get bookings via API returning a 500 error for a specific API key, consider the following steps:

  1. Enhance Error Handling: Ensure the handler function in apps/api/v1/pages/api/bookings/_get.ts is wrapped in a try-catch block to catch and handle exceptions, returning a JSON-formatted error message instead of an HTML response.

  2. Middleware and External Services: Verify that all middleware and external services are configured to return errors in JSON format, as the HTML error response suggests the issue might be outside the handler function.

  3. Server and Application Logs: Review server and application logs for errors or warnings related to the problematic API key to identify where the failure is happening.

  4. API Key Permissions: Double-check that the API key has the correct permissions and access rights to retrieve bookings.

  5. Rate Limiting: Confirm if the 500 error is due to the API key hitting its rate limit. Adjust your application to handle rate limits and provide meaningful responses if a limit is exceeded.

  6. Contact Support: If the issue persists, contact Cal.com support with detailed information, including the request being made and any relevant logs or error messages.

Here's an example modification for better error handling in the handler function:

async function handler(req: NextApiRequest) {
  try {
    // Your logic here...
    const data = await prisma.booking.findMany(args);
    return { bookings: data.map((booking) => schemaBookingReadPublic.parse(booking)) };
  } catch (error) {
    console.error("Error retrieving bookings:", error);
    if (error instanceof RateLimitError) {
      return res.status(429).json({ message: "Rate limit exceeded" });
    }
    throw new HttpError({ message: "An error occurred while retrieving bookings.", statusCode: 500 });
  }
}

This ensures exceptions are caught, and a JSON error response is returned, potentially resolving the issue with the HTML error response and improving the handling of rate limit errors.

#### Details **Sources** - [Cant post to bookings endpoint - status 500](https://github.com/calcom/cal.com/issues/14226) - [packages/app-store/make/api/subscriptions/listBookings.ts](https://github.com/calcom/cal.com/blob/main/packages/app-store/make/api/subscriptions/listBookings.ts) - [apps/api/v1/pages/api/bookings/_get.ts](https://github.com/calcom/cal.com/blob/main/apps/api/v1/pages/api/bookings/_get.ts) - [apps/api/v1/lib/helpers/rateLimitApiKey.ts](https://github.com/calcom/cal.com/blob/main/apps/api/v1/lib/helpers/rateLimitApiKey.ts) - [apps/api/v2/src/ee/bookings/controllers/bookings.controller.ts](https://github.com/calcom/cal.com/blob/main/apps/api/v2/src/ee/bookings/controllers/bookings.controller.ts) - [packages/app-store/zapier/api/subscriptions/listBookings.ts](https://github.com/calcom/cal.com/blob/main/packages/app-store/zapier/api/subscriptions/listBookings.ts) - [packages/platform/constants/permissions.ts](https://github.com/calcom/cal.com/blob/main/packages/platform/constants/permissions.ts) - [apps/web/lib/core/http/error/http-error.ts](https://github.com/calcom/cal.com/blob/main/apps/web/lib/core/http/error/http-error.ts) - [packages/lib/http-error.ts](https://github.com/calcom/cal.com/blob/main/packages/lib/http-error.ts) - [packages/lib/checkRateLimitAndThrowError.test.ts](https://github.com/calcom/cal.com/blob/main/packages/lib/checkRateLimitAndThrowError.test.ts) - [packages/lib/checkRateLimitAndThrowError.ts](https://github.com/calcom/cal.com/blob/main/packages/lib/checkRateLimitAndThrowError.ts) - [packages/lib/rateLimit.ts](https://github.com/calcom/cal.com/blob/main/packages/lib/rateLimit.ts) - [packages/lib/errors.ts](https://github.com/calcom/cal.com/blob/main/packages/lib/errors.ts) - [apps/web/pages/api/sync/helpscout/index.ts](https://github.com/calcom/cal.com/blob/main/apps/web/pages/api/sync/helpscout/index.ts) - [apps/api/v2/src/filters/trpc-exception.filter.ts](https://github.com/calcom/cal.com/blob/main/apps/api/v2/src/filters/trpc-exception.filter.ts)
**About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

keithwillcode commented 1 week ago

@pgvr Are you still experiencing this issue? We've fixed many problems with this endpoint