Ok, many of you probably are NOT aware that Dream Portal has a built in error handling function for your custom modules. So, while there are more ideas on this function for the future, you can begin using it now within your custom modules.
So, you should call this function within your module whenever you'd like to display an error message of some sort. No need to include or require the Subs-DreamPortal.php file, this is done automatically. However, you'll need to return out of your module function after calling this function.
Ok, so let's take a look at the different kind of parameters that this function has and how to use these parameters...
module_error(string type = 'error', string error_type = 'general', boolean log_error = false, boolean echo = true)
So there are 4 parameters for this function:
type:
You can use any of the following predefined strings for this or you can actually type in a string or use a language $txt string from your language file(s).
The following lists the predefined error strings that you can use. By default, if you don't specify this parameter, it will use the predefined 'error' error string with just states that there is an error with the module.
| STRING | OUTPUTTED TEXT |
| error | There is an error with this module! |
| mod_not_installed | Sorry, this mod hasn't been installed yet! |
| not_allowed | Access Denied! |
| no_language | Unable to load the Language file for this mod! |
| query_error | Error trying to obtain information for this mod. Mod must be reinstalled! |
| empty | No content to display for this module! |
Ok, so those are default strings that you can use within the module_error() functions first parameter to output the text. This text is already loaded within the DreamPortal.english.php file.
Again, if you want to use your own text strings, you can pass these strings into the first parameter of the module_error function as well (either the string itself or the $txt variable can be passed into the first parameter).
error_type:
Basically, this is important if you are logging the error into the SMF Error Log (next parameter), but you could also use it to output red text automatically for Critical errors that you define as critical within your DP module.
possible error types to input here are as follows:
general
critical
database
undefined_vars
user
template
debug
If critical is defined for the error type, than the error message will output red colored text as already mentioned.
log_error:
This is a boolean value (either true or false). If true is defined for this parameter, than the error gets logged in the SMF Error log, otherwise, the error does not get logged in the SMF Admin Panel Error Log.
echo:
This is a boolean value (either true or false).
If echo = true will output it directly within the module itself, if false, returns the information to be used within a variable. So, if you want, you can gather all of the different types of errors that occur, place it into an array, and if the array isset() than you can output all of the errors into the module or do whatever you want with those errors.
Well, that is all of the parameters, so now, for an example of how to call it:
if (empty($user_info['id']))
{
module_error('not_allowed', 'critical', true, true);
return;
}
So, basically what this does is it displays an "Access Denied!" error message within the module for all GUESTS that view that module. It colors the text that gets outputted, red. It logs the error within the SMF Admin Error Log, and it echos the text that gets colored red.
Well, if you'd like more examples of how to use this function let me know. Make sure you return out of your modules function after calling the module_error function as I did in the example above.
Well that is basically all there is to the module_error() function. If you want to see how I used it for the Download System Module, download the download system module package and take a look at the script.php file inside of the package for it. The Download System Module is a great example of a Dream Portal Module.
Hope this helps you understand this function more. Ofcourse, if you want to take a look at the function, module_error, yourself within Subs-DreamPortal.php, that would be a good idea as it is a really small function.
Happy Moduling...