Display errors OpenCart - php

In PrestaShop I can display the errors with the function Tools::displayError() like this:
if (empty($email)) {
$this->errors[] = Tools::displayError('Email is empty.');
$this->doLog('ERROR: Email/username is empty');
} elseif (!Validate::isEmail($email)) {
$this->errors[] = Tools::displayError('Invalid email address.');
$this->doLog('ERROR: Invalid Email address');
}
Is there a similar way to do this in OpenCart? Is there a function i can use?
Thanks

To Turn on error reporting, please follow :
Go To Admin Panel
Go to System > Settings
Select your store from the list and click Edit
Go to the Server tab
To display errors, change Display Errors to Yes, If you want to log errors to a file, select Yes for Log Errors
Enter in an Error Log Filename
Click Save
Print Custom error message inside error.log
$logger = new Log('error.log'); //just pass the file name as error.log
$logger->write('Custom Error Message');
You will see the error file inside system-> storage-> logs-> error.log

Related

Laravel How do I return back with SQL errors displayed in HTML?

The problem
I created a constraint in my SQL database to prevent duplicate entries. The initial laravel form submission works fine. I get back the correct message. When I try to throw a duplicate entry, the app, as expected, throws an error.
This is my Controller. A successful form submission does throw the correct error:
$contact->save();
return redirect('contactus.php')->with('status', 'We received your message. We will get back to you soon.');
return back()->withErrors(['Your message may be a duplicate. Did you refresh the page? We blocked that submission. If you feel this was in error, e-mail us or call us.']);
Question
How do I display that error on the HTML screen? Instead of having the page display the following?
Basically the contact form submits information into the database using laravel. When successful, it displays a success message by redirecting it. When not successful, (because of a SQL Unique constraint blocking duplicate entries) so far I've managed to make it throw a SQL error.
How do I display a custom message, like "post not successful, duplicate entry" in that case?
You can do it by using try catch and query exception:
try {
$contact->save();
return redirect('contactus.php')->with('status', 'We received your message. We will get back to you soon.');
} catch(\Illuminate\Database\QueryException $e){
$errorCode = $e->errorInfo[1];
if($errorCode == '1062'){
return back()->with('error', 'Your message may be a duplicate. Did you refresh the page? We blocked that submission. If you feel this was in error, e-mail us or call us.');
}
else{
return back()->with('error', $e->getMessage());
}
}
or another way you can find/check the data first, if already exist just send the error. example:
$contact = Contact::where('email',$request->email)->first();
if($contact)
{
return back()->with('error', 'Your message may be a duplicate. Did you refresh the page? We blocked that submission. If you feel this was in error, e-mail us or call us.');
}
dont forget to get the error on the form view using like below:
<script>
#if(session()->has('error'))
alert('{{session()->get('error')}}')
#endif
</script>

Multiple file input and form validation?

I have multiple files and input text in my form. My code is :
if(formValidationIsGood) {
if(customeCheckFileErrorIsEmpty) {
//StartProcess
}
else {
//I load my view with my array which contains the different error related to the files.
}
else {
//I load my view with the form error (set up by : $this->form_validation->set_rules('variable', 'Variable', 'required');) But if there is an error for the files I cant display it.
}
With this code I can't show the form error and the files error at the same time.
For example the user, is uploading an csv file instead of a pdf file, and he forgot to write into a text input, the form error will be displayed but not the file error and vice versa.
Obviously I am using the helper file provide by code igniter.
Since you want to display error messages for either A OR B, you can try this:
if (!formValidationIsGood || !customeCheckFileErrorIsEmpty)
{
// load the view and display whatever errors you found
}
else
{
// form validation IS good and the files error is empty
}
The if clause above will evaluate to true if either formValidationIsGood ISN'T true (the ! prefix is key) OR the customeCheckFileErrorIsEmpty ISN't true (which would mean there is an error)

Display custom error message in MVC view with PHP

I need to display user friendly error message in the view I am in, and wondering what will be the best solution. I can display and error page using error controller but this is not what i want to achieve. I need to handle all custom error messages in any model and display an error in the view you are in. For example:
I am in "user" controller. When creating new user, the PHP model code checks if same user name exist, if exist I want to display a message in the view or maybe have something like this in header: echo $error; which display any error message I have set to be displayed from any model if occurred.
Example error message in model:
if ($p0 > 0) {
$IsValid = false;
log::LOG_USER_ERROR("This user already exist!", $username);
exit("This user already exist! </br> ");
}
This code write the error in a log file successfully, however how do I display the error message in the same view I am in? exit() displays the message in a blank page. I need to display it as block in red in the same view and design.
exit() terminates the current script, so the code for your View is not executed.
Instead, part of your View should be an area to display messages. Then you can put the error message in a variable (probably an array of messages) that the View displays to the user in that area.

Symfony2 - Show bad credentials error

I have been writing a custom auth provider in Symfony2. Everything works so far, but when I enter a wrong password a get an Internal Server error displaying: "LDAP authentication failed".
Now, this is the message that I want to display, but I'd like to display it above my login form and NOT throw an internal server error. In my listener, I have the following:
try {
$authToken= $this->authenticationManager->authenticate($token);
$this->securityContext->setToken($authToken);
return;
} catch (AuthenticationException $failed) {
throw new BadCredentialsException($failed->getMessage(), 0);
}
So is there anyone who can tell me what I need to do to show the user a message, instead of throwing an internal server error?
Thanks in advance.
You can manually add error to your login form in the controller. For example:
$form->get('username')
->addError(new FormError($message));
You should have a controler action that catch the same path of your form to catch the exception.
authentication process will be tryed before the form will be displayed.
You can see this example :
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller/SecurityController.php

CodeIgniter custom 404 message

I am trying to display an error message on CodeIgniter error page. I am trying this:
Controller/entries.php
public function show_entry()
{
$id = $this->uri->segment(3);
if ($id !== FALSE)
{
..
}
else
{
log_message('error', 'The post ID is missing.');
}
Shouldnt this display my error message 'The post ID is missing' on the CodeIgniter's default 404 error message ie. "The page you requested was not found."
No. The logging class is for writing messages to a log file (ideally somewhere that the user can't read it as it can have information on the inner-workings of your cite). It is something which is really beneficial to you above all.
To display a custom error message, you'll need to either use show_error or, probably more likely in this case, show_404 (both methods documented here).

Categories