Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Bug: not all expected errors are caught and handled internally #206

Open
eredinbg opened this issue Jul 6, 2023 · 0 comments
Open

Bug: not all expected errors are caught and handled internally #206

eredinbg opened this issue Jul 6, 2023 · 0 comments

Comments

@eredinbg
Copy link

eredinbg commented Jul 6, 2023

Steps to Reproduce

  • install the package
  • initialize with createApi
  • use any method to exceed the hourly rate limit

My full code:

import { NextApiRequest, NextApiResponse } from 'next'
import { createApi } from 'unsplash-js'

const UNSPLASH_ACCESS_KEY = process.env.UNSPLASH_ACCESS_KEY

const unsplash = createApi({
  accessKey: UNSPLASH_ACCESS_KEY as string,
})

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  if (req.method !== 'GET') {
    res.status(405).json({ errors: ['Method not allowed'] })
    return
  }

  try {
    const result = await unsplash.search.getPhotos({
      query: req.query.query,
      page: req.query.page,
    })
    switch (result.type) {
      case 'error':
        res.status(result.status).json({ errors: result.errors })
      case 'success':
        res.status(result.status).json({
          ...result.response,
        })
    }
  } catch (error) {
    res.status(500).json({ errors: [error] })
  }
}

Observed Behaviour

Once the hourly limit is reached I'm starting to get a 403 status (not 429 which is strange, but unrelated) response with no JSON body. The lib is throwing a DecodingError instance from here which is being caught by my catch block and not internally, therefore, it lacks valuable original response data.

Expected Behaviour

DecodingError should be caught and handled here internally. My guess is that getJsonResponse should return Promise.reject(new DecodingError('expected JSON response from server.')); rather than throwing it for the error to be caught by the following catch method in the original Promise chain.

Technical Notes

Stack used:

  • Node.js: 18.14.0
  • pnpm: 8.6.6
  • Next.js: 13.4.8
  • unsplash-api: 7.0.18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant