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

[bug] Why is an error raised for a bad HTTP status instead of the handler being called? #100

Open
delano opened this issue Mar 27, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@delano
Copy link
Contributor

delano commented Mar 27, 2024

Relevant code

From pages/login/index.vue:

try {
  // Send post request to the API endpoint using Nuxt 3 useFetch
  const path = "/api/passwordless/auth/email/";
  await $fetch(path, {
    onRequest({ request, options }) {
      console.log("Request:", request);
      // Set the request headers
      options.headers = options.headers || {};

      // Addresses the following TypeScript error:
      //   Element implicitly has an 'any' type because expression of type '"Content-Type"' can't be used to index type 'HeadersInit'.
      //
      // TODO: Find a better way.
      (options.headers as { [key: string]: any })["AFB"] = "rules";
    },
    onRequestError({ request, options, error }) {
      // Handle the request errors
      console.error("A request error occurred:", error);
    },
    onResponse({ request, response, options }) {
      const data = response._data;
      console.log("Response data:", data);
      // Process the response data

      // A successful response returns a friendly message for the
      // user. The next step is for the user to check their email
      // for the magic link to sign in.
      if (response.ok) {
        // Ignore data.detail bc it's not a friendly message
        // and comes straight from drfpasswordless.
        const message = "Check your email for a link to log in.";
        console.log("Message:", message);

        snackbar.add({
          type: "success",
          text: message,
        });
      } else {
        // Handle the response errors
        console.error(
          "A response error occurred (1):",
          "Status code:",
          response.status,
          "Status message:",
          data.detail,
        );

        snackbar.add({
          type: "error",
          text: "An error occurred. Please try again.",
        });
      }
    },
    onResponseError({ request, response, options }) {
      // Handle the response errors
      console.error(
        "A response error occurred (1):",
        "Status code:",
        response.status,
        "Status message:",
        response._data.detail,
      );
    },
    baseURL: config.public.apiBase,
    method: "POST",
    body: JSON.stringify(payload),
    headers: {
      "Content-Type": "application/json",
    },
    mode: "cors",
  });
} catch (error) {
  console.error("An unhandled error occurred:", error);
}

Context

index.vue:98 Response data: 

POST https://dev.afb.pet/api/passwordless/auth/email/ 400 (Bad Request)
{
    "email": [
        "Enter a valid email address."
    ]
}

Screenshot 2024-03-27 at 11 15 11

Screenshot 2024-03-27 at 11 10 18

@delano delano self-assigned this Mar 27, 2024
@delano delano added the bug Something isn't working label Mar 27, 2024
@delano delano moved this from 🆕 Triage to 📋 Backlog in AFB Requests MVP - June 2024 Mar 27, 2024
@delano delano changed the title [bug] Why is an error raised for a bad HTTP status instead of the heandler being called? [bug] Why is an error raised for a bad HTTP status instead of the handler being called? Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: 📋 Backlog
Development

No branches or pull requests

1 participant