import type * as t from '@/api/ius/types';
import { HttpError } from '@repo/ui';
import type { WretchError } from 'wretch/types';

type Props = {
  render: () => Promise<JSX.Element>;
};

export async function withError({ render }: Props) {
  try {
    return await render();
  } catch (error) {
    const _error = error as WretchError;
    const json = _error.json as t.HttpError;

    return (
      <HttpError
        code={_error.status.toString()}
        title={json?.title || _error.name}
        description={json?.errors?.join('') || _error.message || _error.text}
      />
    );
  }
}
