Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  88] [ 7]  / answers: 1 / hits: 28190  / 7 Years ago, sat, may 27, 2017, 12:00:00

Still quite inexperienced and I'm trying to error handle a 400 Bad Request.



I have a site with a search bar.

The value entered into the search bar is then placed into an api url that returns an object.

Whenever a misspelled search value is entered, the site's console returns a 400 Bad Request for the api url.

I also receive the error object below from the api url request.



{
meta: {
code: 400,
errorType: failed_geocode,
errorDetail: Couldn't geocode param near: Jljjl,
requestId: 59208ac96a6071641949481d
},
response: {}
}


What I want to do is use a conditional statement like below to handle this error:



try {
if (400 Bad Request) throw incorrect;
} catch (err) {
document.getElementById('results').innerHTML = Input is + err;
}


I've tried conditional statements like the one's below but it seems like I am unable to access any of the values in the error object that is returned:



if (object.meta.code === 400)
if (object.meta.code !== 200)
if (object === undefined) // or null or 0


How can I put the 400 Bad Request error into the if statement, or is there another way to handle these errors?

Thanks


More From » javascript

 Answers
9

Just to piggy back off of m1kael this is what I have



window.fetch('https://api.foursquare.com/v2/venues/explore?v=20170324&near=Jljjl&query=study%20spot&limit=10&intent=browse&radius=100000&venuePhotos=1&client_id=XPQI2RJB3NMDT2JYQIMWDMGZSKRDQZX40NYIOKK02DXB1CVE&client_secret=UNHLLMUWXTEI3USTTN5DRJ02QDWQMHZQ5B22CFX0EY4JLEHO')
.then(r => r.json())
.then(r => {
if (r.meta.code === 400) {
console.error('Error')
} else {
console.log(r)
}
})

[#57639] Thursday, May 25, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sidneyh

Total Points: 118
Total Questions: 108
Total Answers: 105

Location: Mali
Member since Fri, Jun 18, 2021
3 Years ago
sidneyh questions
Tue, Jun 7, 22, 00:00, 2 Years ago
Wed, Apr 13, 22, 00:00, 2 Years ago
Wed, Aug 12, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Fri, Apr 24, 20, 00:00, 4 Years ago
;