Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant