CakePHP redirecting on error - cakephp

I'm receiving a strange error from my call to query() the database. This normally wouldn't be a problem, except that it seems that the query call is redirecting me to the 500 server error page and not executing the rest of my code.
Regardless of the error, this PHP is executed during an AJAX call, meaning that I'm unable to return a properly formatted json string containing the error because CakePHP is redirecting.
A workaround would be to just parse the big HTML response from the server if there is an error, and use javascript to figure out what went wrong and display it properly, but I would much rather return a little json string from the backend that plays nicely with the code I already have in place.
Here is my call:
$this->FileUpload->query('exec sproc_runjob_ProcessTests', false);
Any help would be much appreciated. Thanks in advance!

As bfavaretto mentions above, the correct answer is to surround the query line in a try catch.
Here is the code I used:
try{
$this->dbParent->FileUpload->query('exec sproc_runjob_ProcessTests', false);
} catch(PDOException $e){
if($e['errorInfo'][0] == 42000 && $e['errorInfo'][1] == 22022)
return array('error' => 'sproc_blocked');
}
return array('success'=>true);

Related

Web api is giving error on passing * as the input value to the api method parameter

I am using asp.net mvc web api and i have this method
[HttpGet]
public LoginResult AuthenticateOnlineBookingUser(String userName,String password)
{
//My Code
}
The problem is that when i pass (*) as input value to the parameter (password)
i receieve this error but on other inputs it is working perfectly
A potentialy dangerous Request.Path.value was detected from client(*)
Thanks in advance
Note:My client side is written in angular js
i tried this solution as well Getting "A potentially dangerous Request.Path value was detected from the client (&)" but it is not working for me
You need to set the options for invalid characters. You can do this in your web.config as shown here.
Use url encoder to encode the request before sending it to server.
Finally solved my problem by changing my GET request to POST request The problem was with query string in Order to solve it with GET Request i have to make some changes to my query string in order to make it work but

php file() triggers no Exception on valid URL

try {
$antwort = file_get_contents('http://not_existing.notnotnot', false);
if($antwort===false) echo 'ERROR';
} catch(Exception $e) {
$e->getMessage();
}
var_dump($antwort); // returns string(0) ""
I get no Exception, no false, just empty content for every URL. A valid URL returns with this snippet the right content. Why can't I get exceptions for an invalid URL?
I came to this question because a wget on the same server leads to a valid return, but with a php script I can't file() the same URL. Really weird and I have no idea how to debug it.
It won't throw an exception if the file isn't found; it will raise a warning-level error. Those are different things. From the docs:
An E_WARNING level error is generated if filename cannot be found, maxlength is less than zero, or if seeking to the specified offset in the stream fails.
You should check for a false return, as you do, and not expect to catch an exception.
Also keep in mind when fetching a URL that the remote server may return an incorrect status code (instead of the expected 404), causing your script to think the file exists when it does not. You may need to check for empty values ("") as well.
As a rule, you should avoid using file_get_contents to access files via HTTP. It's not terribly secure, and many hosts don't even allow you to use it that way. Instead, use cURL, which is specifically designed for retrieving data over the web, including via HTTP.

Backbone not sending PUT to server

I am having trouble with trying to get backbone to send a PUT request out to my rails server on save. I am not sure what I am doing wrong here, it is fine with GET requests just not PUT..
Here is the code that is in my view that I use to save my model.
e.preventDefault()
$(#el).find('#error_explanation').html ""
data = Backbone.Syphon.serialize(this)
setError = false
#model.set(data, error: (model, error) ->
setError = true
)
#model.save()
Any help would be greatly appreciated.
Thanks,
-do
Stupid me....
Here is what was in my model.
validate: ->
console.log "validating"
console.log "options: #{options}"
errors = []
if(!#.validateEmail(#.get('email')))
errors.push("Email can't be blank")
if(!#.validateName(#.get('first_name')))
errors.push("First name can't be blank")
if(!#.validateName(#.get('last_name')))
errors.push("Last name can't be blank")
return errors
This was actually being called when I was trying to save it which I was just trying to call manually. Since I wasn't returning the correct data saying that it was valid/invalid it just was always invalid.
ooops...
-do

CakePHP Cakemenu plugin fails after global error due to incorrect string encoding

I am using CakePHP 2.1.2 with PHP 5.3.5 and a plugin called 'Cakemenu' which normally works fine. The plugin stores menus in a db table with the menu link stored as text like
array('plugin'=>null,'controller'=>'assets','action'=>'index');
The helper in the plugin gets those values, then executes this code to convert that text to an array:
//Try to evaluate the link (if starts with array)
if (eregi('^array', $value['Menu']['link'])) {
$code = "\$parse = " . $value['Menu']['link'] . ";";
$result = eval($code);
if (is_array($parse)) {
$value['Menu']['link'] = $parse;
}
}
Everything works fine unless CakePHP is handling an error. For example if I mistype the name of a controller in the browser I should get a menu and then the missing controller message. Instead I get a page full "Parse error: syntax error, unexpected $end in..." messages pointing to the line with the eval statement. If I printout the variable that is getting eval'ed I see that it has been (incorrectly) encoded with Html entities when it normally does not.
Good string to be eval'ed:
$parse = array('plugin'=>null,'controller'=>'assets','action'=>'index');
Bad string to be eval'ed:
$parse = array('plugin'=>null,'controller'=>'Parts','action'=>'add');
To temporarily fix the problem I added two statements to just replace the offending characters
$value['Menu']['link'] = str_replace( ''','\'',$value['Menu']['link']);
$value['Menu']['link'] = str_replace( '>','>',$value['Menu']['link']);
and everything works great again. Some other pieces of information that might be helpful is that the array of data used to generate the menu is read during the beforeFilter of the app and saved in a view variable and then the menu is generated as an element in the view.
I'm thinking that the error causes CakePHP (or PHP) to skip some loading or configuration process and that causes the string to be mishandled. Any help would be appreciated, thanks
Your beforeFilter() method won't be executed on error pages. You'll have to handle your errors yourself and manually call beforeFilter(). I wrote a blog post on how to use custom error pages - pay close attention to the Controller Callbacks section.

CakePHP - Miles Uploader Plugin - Isset Error

I have tried several CakePHP file uploaders, but have not been successful' in setting up and making it work. I've settled down with Miles Uploader Plugin and I am doing everything I can to make it work. Installation instructions are excellent and I have followed them to the letter, but I still cant get it to work.
When I post an Article I get the following error:
Warning (2): Illegal offset type in isset or empty [APP/plugins/uploader/controllers/components/uploader.php, line 1104]
}
if (isset($this->_data[$file])) {
I am making the following call in my add articles view
...
echo $form->input('main_image', array('type' => 'file'));
...
And I am using the following in my add action but it is returning false
...
if (!empty($this->data)) {
//This call is not successful which skips to the }else{ display the message below
if ($data = $this->Uploader->upload('main_image')) {
// Upload successful, do whatever
}else{
$this->setFlash('Uploader Error',true);
}
}
...
Can anyone please shed some light on what could possibly be wrong.
** EDIT **
I am not able to try this out right now, but do I need to do the following to make it work:
if ($data = $this->Uploader->upload(data['Article']['main_image'])) {
Thanks,
Spent many hours trying to figure out why it does not work. Finally I gave up last night trying and settled with trying to configure CakePHP's Media Plugin. That now works great. Setup was a breeze.

Resources