NeERAJ TK
NeERAJ TK

Reputation: 2695

Unexpected token, expected ")" inside axios try-catch statement

I'm getting a strange issue Unexpected token, expected ")" when trying to add the type as any of the exception e in the try-catch statement. Its working fine when any type is removed from the catch block. (Adding any explicitly as without it git pipeline fails)

 try {
      await axios.patch(
        "/scriptschedule/" + pk + "/",
        postData
      );
      setSubmitting(false);
      afterCreateOrUpdateAction();
    } catch (e: any) {
      setSubmitting(false);
      let message =
        e.response.data && e.response.data.non_field_errors
          ? e.response.data.non_field_errors
          : "Error. Please try again";
      addToast({
        id: "1",
        title: "Error",
        color: "danger",
        text: <p>{message}</p>,
      });
    }

enter image description here

The syntax looks fine to me. Am I missing something?

Upvotes: 2

Views: 672

Answers (1)

Ashley
Ashley

Reputation: 579

It's not possible to put a type annotation on the error in the catch clause. The solution to this would be to cast the variable (as I believe you've done).

try { 
  // ...
} catch (e) {
  (e as any)...
}

Upvotes: 4

Related Questions