Repository: apache/airflow
File: airflow-core/src/airflow/ui/src/queries/useTrigger.ts (TypeScript)

Code before:
    // Only redirect if we're already on the dag page
    if (selectedDagId === dagRun.dag_id) {
      navigate(`/dags/${dagRun.dag_id}/runs/${dagRun.dag_run_id}`);
    }
  };

  const onError = (_error: unknown) => {
    // Check if error is a 403 (Forbidden) status
    if (isHttpError(_error, 403)) {
      toaster.create({
        description: translate(
          "triggerDag.toaster.error.forbidden.description",
          "You don't have permission to trigger this DAG",
        ),
        title: translate("triggerDag.toaster.error.forbidden.title", "Permission Denied"),
        type: "error",
      });
    } else if (isHttpError(_error, 401)) {
      toaster.create({
        description: translate(
          "triggerDag.toaster.error.unauthorized.description",
          "Please log in to trigger DAGs",
        ),
        title: translate("triggerDag.toaster.error.unauthorized.title", "Authentication Required"),
        type: "error",
      });
    } else {
      // Generic error handling for other status codes
      toaster.create({
        description: translate("triggerDag.toaster.error.generic.description", "Failed to trigger DAG"),
        title: translate("triggerDag.toaster.error.generic.title", "Error"),
        type: "error",
      });
    }
    setError(_error);
  };

  const { isPending, mutate } = useDagRunServiceTriggerDagRun({
    onError,
    onSuccess,
  });

  const triggerDagRun = (dagRunRequestBody: DagRunTriggerParams) => {
    const parsedConfig = JSON.parse(dagRunRequestBody.conf) as Record<string, unknown>;

    const logicalDate = dagRunRequestBody.logicalDate ? new Date(dagRunRequestBody.logicalDate) : undefined;

    // eslint-disable-next-line unicorn/no-null
    const formattedLogicalDate = logicalDate?.toISOString() ?? null;

    const checkDagRunId = dagRunRequestBody.dagRunId === "" ? undefined : dagRunRequestBody.dagRunId;

Review comment (suggestion):
This should almost never happen, user is using the UI, token is automatically refreshed, so I don't see how we could end up with a 401 response just for triggering the run. (The whole UI would 401)

Code after:
    if (selectedDagId === dagRun.dag_id) {
      navigate(`/dags/${dagRun.dag_id}/runs/${dagRun.dag_run_id}`);
    }
  };

  const onError = (_error: Error) => {
    toaster.create({
      description: _error.message,
      title: translate("triggerDag.toaster.error.title"),
      type: "error",
    });
    setError(_error);
  };

  const { isPending, mutate } = useDagRunServiceTriggerDagRun({
    onError,
    onSuccess,
  });

  const triggerDagRun = (dagRunRequestBody: DagRunTriggerParams) => {
    const parsedConfig = JSON.parse(dagRunRequestBody.conf) as Record<string, unknown>;

    const logicalDate = dagRunRequestBody.logicalDate ? new Date(dagRunRequestBody.logicalDate) : undefined;

    // eslint-disable-next-line unicorn/no-null
    const formattedLogicalDate = logicalDate?.toISOString() ?? null;

    const checkDagRunId = dagRunRequestBody.dagRunId === "" ? undefined : dagRunRequestBody.dagRunId;
    const checkNote = dagRunRequestBody.note === "" ? undefined : dagRunRequestBody.note;

    mutate({
      dagId,
      requestBody: {
        conf: parsedConfig,
        dag_run_id: checkDagRunId,
        logical_date: formattedLogicalDate,
        note: checkNote,
        partition_key: dagRunRequestBody.partitionKey ?? null,
      },
    });
  };

  return {
    error,
    isPending,
    triggerDagRun,
  };
};