clearbit / clearbit-node

Node library for querying the Clearbit business intelligence APIs
https://clearbit.com/docs
MIT License
69 stars 35 forks source link

PersonNotFound does not have an error type #24

Open alexlatchford opened 7 years ago

alexlatchford commented 7 years ago

Just parsing through the various errors we get back from a bunch of calls and looks like "PersonNotFound" returns with:

{"message":"Resource not found"}

All the other errors seem like they have an error.type property I can check and/or a statusCode attribute. Any chance we can add this in to make this one consistent, I'll match against the message for now :)

Thanks, Alex

maccman commented 7 years ago

Oops - will fix this.

tommedema commented 3 years ago

This has not been resolved -- due to this issue we have to apply some pretty ugly workaround code such as:

/**
 * It is necessary to perform a conditional type or name check to determine
 * if a clearbit email was invalid or not found.
 * @see https://github.com/clearbit/clearbit-node/issues/24
 */
const clearbitNoResultErrorTypes = ['email_invalid'] as const
const clearbitNoResultErrorNames = ['EnrichmentNotFoundError'] as const

interface IClearbitNoResultError {
  type?: typeof clearbitNoResultErrorTypes
  name: typeof clearbitNoResultErrorNames | 'ClearbitError'
}

const isClearbitNoResultError = (err: any): err is IClearbitNoResultError =>
  clearbitNoResultErrorTypes.includes(err.type) || clearbitNoResultErrorNames.includes(err.name)

try {
      const { company, person } = await clearbitClient.Enrichment.find({ email, stream: true })

      if (company || person) {
        console.log('enriched email %s with clearbit to company %o and person %o', email, company, person)
      }
    } catch (e) {
      if (!isClearbitNoResultError(e)) {
        throw e
      }
      else {
        console.log('no results found for email: ' + email)
      }
    }