-
Notifications
You must be signed in to change notification settings - Fork 799
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
Error handling #247
Comments
There does need to be better handling. Also, this is just a raw library that wraps curl. It doesn't have any nice methods like You can easily create your own object which decorates this one and enforces custom validation and constraints. |
Is there a way I can wrap any of this in a try/catch? |
You can enforce the keys being provided at the object level for a start (never instantiate an object in an invalid state). Let's say we're updating a status, let's look at the docs. For example, we can see that
... and then you catch the exception and show a nice error message to the user, although really you should be validating the length on the client-side too, so really it's an arbitrary example. Then, if you want to reply to a tweet...
The responses from twitter aren't very helpful but the above does what you want... |
How do i force the keys to be required, when I am letting the user define them? This is what my original post was about - if a key is missing, how am I best dealing with that scenario? |
When you say keys, are you referring to the authentication tokens? Or array keys required to make the API work, like a status key / value pair? |
The authentication tokens. I'm letting the user put these in, so it maybe that somehow one is missing (yes, I know I could make them required etc). It was about that, and then a more general how to deal with errors, and I'm struggling to make any try/catch statements work (not sure where they should be placed etc) |
If this is just a generic PHP problem with understanding how to utilise exception handlers and try / catches, then I can try and help but you may get faster responses from StackOverflow. However if this is specific to this library... then the problem isn't that you need to enforce the users providing them.. there are four different keys / tokens. They must all be provided. You can enforce this in the front end by not submitting data to the server unless all four fields are filled and look like valid tokens. Then you also enforce this on the back end by making sure exactly four variables are passed in via POST. Your main problem is ensuring that these tokens work. You can test this by trying a simple GET request (this is tested in the library, see Is that helpful? |
Ok, so here is my code: Twitter class
Plugin code:
Template code:
So, with a missing key, the response from twitter is an error message. But because I am trying to spit out If I want to catch the error in the |
Firstly you need to make sure in your constructor that if any of the |
Also with the property of non-object, you can easily do a |
I tried that, but I get an uncaught exception error. in the class file
in the plugin file
|
Try using |
Sorry, it is, small typo, but i do have it as that |
This plugin file looks like it's wordpress or something? I can't help with third party solutions, but it's obvious that throwing an PS your code says |
Hmm, balls! Yes, it's WordPress, but I'd have assumed throwing a normal PHP exception would be fine :( |
I'd highly recommend getting this thing working entirely outside of wordpress first. It's exactly the same with frameworks. Build your solution as agnostic as possible. As soon as you get it working, then your only job is to 'slot' it in, and that's the easiest bit. Once you get to the slotting it in stage, your first thing to figure out is absolutely nothing to do with this library, or code. You want to figure out why throwing a simple exception isn't caught. No other code should be used, just an exception. Solve it step by step |
Agreed. It looks like WP deals with exceptions in an odd way :( |
I'm going to close this as a third-party (wordpress-specific) problem, but if you need any help directly with the library then please feel free to re-open / create another issue. |
How would I go about adding error handling into this?
Let's say one of the keys is not provided, and you cannot connect to the API, you get an error object back.
But I'm struggling to add logic into my code that lets me handle the error and spit out a nice message on the frontend of my site.
The text was updated successfully, but these errors were encountered: