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

How to do error handling? #21

Open
sascha-hendel opened this issue Mar 29, 2018 · 5 comments
Open

How to do error handling? #21

sascha-hendel opened this issue Mar 29, 2018 · 5 comments

Comments

@sascha-hendel
Copy link
Contributor

I tried the examples. Works well, if all input, etc is correct, but if something is not the way it should be, PHP exit with error. Is there any suggested way to catch errors/do controlled error handling?

@kenguest
Copy link
Member

Just wrapping your "client" code with the usual try/catch/finally statements should work.

For example:

$osm = new Services_OpenStreetMap();

try {
    var_dump($osm->getCoordsOfPlace("Nena, Irelande"));
} catch(Exception $ex) {
    var_dump ($ex->getMessage());
} finally {
    echo "yay!\n";
}

@sascha-hendel
Copy link
Contributor Author

Ok, this method works, but it is pretty hardcore...
It would be better, if for example $changeset->commit() would return a statuscode/message with predefined status levels.

@kenguest
Copy link
Member

Hi @sascha-hendel

I don't know how catching exceptions in PHP could be called hardcode, but I think you're correct that there are some places in Services_Openstreetmap where returning a simple value or array of values would be better than throwing a new exception.

Strictly speaking, exceptions should be for "exceptional scenarios" - something not easily predictable happening, for example.
So in the example above, getCoordsOfPlace quite possibly should be returning false when it can't get the lat/lon for a named place that doesn't exist. BUT if there are connectivity issues, exceptions should be thrown.

Are there any specific cases where you think it would be easier for you that exceptions weren't being thrown? I don't want to start making changes that would break compatibility but I will consider this in future.

@sascha-hendel
Copy link
Contributor Author

Hi Ken,
you are are right, that for connectivity issues, etc it is best to throw an exception.
But I miss feedback for cases, where OSM throws an error. For example a $changeset->commit() should return an info, if the process was succesful or not.

@kenguest
Copy link
Member

Hi @sascha-hendel, the changeset commit method now finally returns true if changes have been successfully applied.

(The API itself specifies that nothing is returned upon successful closing of a changeset, so this is just a check for a Response Code of 200)

Is there anything else about this that you might like to see improved?

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

2 participants