I would like to create a custom error page in Drupal 7. There are things like set_message, but they don't log the errors. So is there any hook or something similar to catch the error, log it and display a human error to my users?
You stated above that your goal is "catch the error, log it and display a human error to my users".
In that case you're probably looking for the functionality Try/Catch which allows you to try to run a block of code and if something goes wrong it will display a message.
In your particular case you can log the error to Drupal's database logging system with the watchdog function http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/watchdog/7
Optionally you could also log this to the PHP error_log as well see http://php.net/manual/en/function.error-log.php
Then you could continue on displaying the message to the user using the drupal_set_message function that you already figured out.
The final code for what you're trying to accomplish would look something like this:
try {
// RUN YOUR CUSTOM CODE HERE
} catch (Exception $e) {
// Record the error Drupal's database log
watchdog('error_page', $e->getMessage());
// Record the error to PHP's error_log
error_log($e->getMessage());
// Display a message to the user
drupal_set_message("We're sorry, but we couldn't find the page you were looking for.", 'error');
}
drupal_get_messages() could be used to fetch an array to iterate through for the the error types of messages.
I'm not sure I made 100% sense of your question, but since you referenced drupal_set_message() i thought this might be what you were looking for.
You could handle it in hook_init(), check for messages there, if you find any, do something with it.
Redirecting on an error though could potentially break default drupal functionality like forms.
Related
When I develop templates in Sulu 1.6 and an error occurs, where can I find the full error or exact error location? Is there some kind of special log or do I need to change the default location?
The corresponding /admin/websocket/admin response contains only this (basically the same information):
{
"handler":"sulu_preview.preview",
"message":{
"code":9903,
"message":"Unclosed \"block\".",
"type":"Sulu\\Bundle\\PreviewBundle\\Preview\\Exception\\TwigException"
},
"options":[],
"error":true
}
Currently I only see messages like this:
It would be also helpful to know, where the catch block for this is, in case it is not jet implemented.
Thx a lot!
So it turned out, that there is no logging enabled, and because the PreviewRendere throws a new Exception, the original information is pretty much lost. It can be added in this way: https://github.com/sulu/sulu/pull/4363
Best wishes
Andreas
I need to throw 404 error in module. Or may be there are any possibility to set required option for menu hook?
It is easy. These should take care of watchdog, HTTP 404 response code and other related things.
For Drupal 6 & 7
In your module's page callback, do: return drupal_not_found();
For Drupal 8
In the class::method() referred to in the _controller definition (i.e. the page callback or the method responsible for generating output for the request), do:
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
References
drupal_not_found() documentation for Drupal 6.
drupal_not_found() documentation for Drupal 7.
NotFoundHttpException documentation for Drupal 8.
For Drupal 8+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
// then
throw new NotFoundHttpException();
MENU_NOT_FOUND should be returned in page callback functions.
Page callback functions wanting to report a "page not found" message should return MENU_NOT_FOUND instead of calling drupal_not_found().
— http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_not_found/7
Look into the drupal_add_http_header() function to play with the HTTP header attributes. Also make sure you stick this at the top of your module's code to make sure it executes first. Also, you might find this helpful. https://www.drupal.org/project/generate_errors
I am working on yet another module idea. This module is simply supposed to redirect a user automatically to a specified node like in the following example code:
function test_module_init(){
drupal_goto('node/100');
}
The code is a mere example. But, the very really problem is the code keeps executing until the browser returns a "too many redirects" error message.
I understand why this is happening. What I need help with is the best hook to place my drupal_goto code so it executes once instead of the multiple times it currently does in hook_init.
Can't you just do something like this:
function test_module_init() {
if(isset($_GET['q') && $_GET['q'] == 'node/100') {
// skip goto statement
return;
}
drupal_goto('node/100');
}
The init hook you wrote fires on every page load including when you are on node/100. That's why you're getting a redirect. So the page is redirecting to itself. You really only need to redirect if you're not already on node/100. You can find this info in $_GET['q'] if you need to.
You don't need a different hook you just need to make sure that you don't call the drupal_goto if you're already on the destination page.
This error suddenly occurred when Pressflow was added to our Drupal installation. It is coming from a custom module that, prior to Pressflow seemed to work fine. After the addition of Pressflow, running Drush causes this error to display on screen.
The function that is identified as the source of the error looks like this:
function user_search_location_get(&$user) {
if (count($user->user_location_pref)) { // This line is causing the error.
return $user->user_location_pref;
}
// …
}
The error message is the following:
WD php: Notice: Undefined property: stdClass::$user_location_pref in user_search_location_get()
Short answer, in your custom module, you should check if that property exists before you count it. That, or make sure the $user object has that property before you use it.
if (isset($user->user_location_pref) && count($user->user_locaion_pref) > 0) {
return $user->user_locaion_pref;
}
While it is a little more work, when you start developing with notices turned on, you will find errors in your code that otherwise would have not appeared till later, and would have been more difficult to track down.
In your previous environment or install, the PHP error reporting was probably set to not show notices. While I recommend keeping notices on and making your code work with them, you can turn them off through the Drupal 7 UI. Configuration -> Development -> Logging and Errors. Then set the value to 'Errors and Warnings' ... Otherwise, you can set your error reporting level in your php.ini to report all except notices.
Note that Drupal 6 did not force notice reporting on, while Drupal 7 does. That prompts this type of question a lot.
If this is your only notice issue though, it makes more sense to just correct your custom module.
What is the correct way to force the system to send an invoice to a client. I'm trying to use:
uc_order_action_email($order, $settings);
But I keep getting:
Fatal error: Call to undefined function uc_price() in C:\xampp\htdocs\YourEstablishment\src\sites\all\modules\ubercart\payment\uc_payment\uc_payment.module on line 149
It might be a flaw in the module. The function that it's complaining about, uc_price, is defined in
ubercart/uc_store/includes/uc_price.ini
Since it's located in a ini file, that means that drupal wont include it by it self. I'm not familiar with ubercart, since I've never used it, but it seems like this could be a bug in the module. If no one here can come up with an explanation, you should go to the issue tracker.
A quick fix to your problem would be to add this before you call the function
require_once(drupal_get_path('module', 'uc_store') . '/includes/uc_price.inc');
it will include the needed file.