import { IntegrationError } from './types'

export async function throwErrorInterceptor(response: Response) {
  const clonedResponse = response.clone()
  const data = await clonedResponse.json()

  if (data?.id?.startsWith('err_')) {
    const error = new Error(data.message) as Error & IntegrationError
    error.id = data.id
    error.code = data.code
    error.type = data.type
    throw error
  }

  return response
}
