I am making a request to an API that gives me some basic account details.
If anything fails connecting to the endpoint, the try catch works correctly and it executes the catch block.
But I want to throw an error depending on the data I am getting, but it defaults to the catch block. How can I do this?
Here is a mockup of what I am trying to achieve.
import axios from 'axios'
export default async () => {
try {
const { data } = await axios('https://api.endpoint.com')
if (data.credits < 10) {
throw 'No enough credits'
} else {
return 'All good'
}
} catch (error) {
throw 'Error connecting...'
}
}