Skip to content

Handle 500 errors from /route - #103

Merged
easbar merged 8 commits into
masterfrom
issue_70
Jun 10, 2021
Merged

easbar merged 8 commits into
masterfrom
issue_70

Conversation

@easbar

@easbar easbar commented Jun 10, 2021 •

Copy link
Copy Markdown
Member

Fixes #70
The issue is not strictly related to alternative routes, but whenever the GH API times out and we receive 500 our code failed because it was assuming there is a hints field in the error response that we only get for 400 errors.

Comment thread src/api/Api.ts Outdated
paths: ApiImpl.decodeResult(rawResult, completeRequest.elevation),
}
} else if (response.status !== 400) {
throw new Error(response.statusText)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The GraphHopper API returns 500 when a request times out, so when this happens we see `Internal Server Error' for example. Not sure if this is too unfriendly for users and what else we should do. Maybe 'Unexpected Error' or even 'The request timed out'? In most cases 500 should be because the request timed out and even when the reason was something else we could still show this message? And maybe log the actual error to the console?

Comment thread src/api/Api.ts Outdated
Comment on lines +102 to +106
console.log(`route request failed, status: '${response.statusText}'`)
if (response.status === 500)
// not always true, but most of the time :)
throw new Error('Route calculation timed out')
throw new Error('Route request failed')

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After thinking about this again I decided to use these more user-friendly error messages. WDYT? And should we translate the error messages (already?)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have translation now, I'd say yes, if we have an appropriate translation.

I find the different paths of the else if block not easy to read. isn't it somethig along the lines like

if (response.ok) {...}
else if (response.status === 500) {
    throw new Error('Route calculation timed out')
} else if (response.status === 400) {
    throw new Error('Insert your meaningful bad request message here')
} else {
    console.log(`route request failed, status: '${response.statusText}'`)
    throw new Error('Route request failed.')
}
    

@karussell karussell Jun 10, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we have an appropriate translation

Everyone can add translations here and we can use them now here too, yes :)

Add text to English at least (and maybe German) and prefix the key with web. and then run python3 update-translations.py.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cleaned up the if/else logic and added the translations (this worked right away 👍)

@Janekdererste Janekdererste left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Friendlier user messages.

Comment thread src/api/Api.ts Outdated
Comment on lines +102 to +106
console.log(`route request failed, status: '${response.statusText}'`)
if (response.status === 500)
// not always true, but most of the time :)
throw new Error('Route calculation timed out')
throw new Error('Route request failed')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have translation now, I'd say yes, if we have an appropriate translation.

I find the different paths of the else if block not easy to read. isn't it somethig along the lines like

if (response.ok) {...}
else if (response.status === 500) {
    throw new Error('Route calculation timed out')
} else if (response.status === 400) {
    throw new Error('Insert your meaningful bad request message here')
} else {
    console.log(`route request failed, status: '${response.statusText}'`)
    throw new Error('Route request failed.')
}
    

Comment thread src/api/Api.ts Outdated
} from '@/api/graphhopper'
import { LineString } from 'geojson'
import { getTranslation } from '@/translation/Translation'
const tr = getTranslation()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not ideal, because just loading the file executes getTranslation() and this fails if there was no setTranslation() before. Why do we even need getTranslation()? Can we not keep the Translation object local to Translation.ts and export only the tr function instead?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a good idea, yes. I played around and wasn't sure how to integrate this properly to make it convenient but also aligned to other code. My biggest problem with it is that this is a singleton but probably this is fine for a web app :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you take a look at my last commit to see if you like it?

@easbar easbar Jun 10, 2021 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what is your concern about the singleton? I think tr.json should only be loaded once (this data is global so to say). Other than that all we need is some handy access function to retrieve the data. We also need to keep some state to keep track which is the current language. This is handled by the Translation object currently and it makes sense there is only one such object?

@karussell karussell Jun 10, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what is your concern about the singleton?

I just dislike singletons in general (probably from my previous life as Android dev where it made everything a mess :D )

Can you take a look at my last commit to see if you like it?

Looks even more convenient, yes.

The mechanism is not really changed though. I.e. if setTranslation is not called before the usage you'll still get an error (?)

@easbar easbar Jun 10, 2021 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mechanism is not really changed though. I.e. if setTranslation is not called before the usage you'll still get an error

Yes. setTranslation still needs to be called before using it (makes sense). The problem was that we put const tr = getTranslation() at the beginning of some files. I did this because otherwise I would have to do getTranslation().get(..) wherever I wanted to translate something. But on the other hand if we put const tr = getTranslation at the beginning of the file this is executed as soon as the file is loaded and the order of the import statements becomes relevant which I thought was ugly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok 👍

Comment thread src/translation/Translation.ts Resolved
@easbar
easbar merged commit 860318d into master Jun 10, 2021
@easbar
easbar deleted the issue_70 branch June 10, 2021 12:00
@karussell

Copy link
Copy Markdown
Member

Nice @easbar !

easbar added a commit that referenced this pull request Jun 11, 2021
ZeroGxMax pushed a commit to minhhpkp/graphhopper-maps that referenced this pull request Nov 12, 2024
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

Successfully merging this pull request may close these issues.

server side timeout of alternative routes results in JS error

3 participants