1

So I've wasted a day because I couldn't figure out how to get an error back from an action I'm using in a plugin...

$newUserSuccess = craft()->users->saveUser($newUser);

$newUserSuccess was empty, and it turns out the reason was because the new user was trying to register with an existing user's email address, which fails. I could not figure out how to get any useful information back. I had tried this:

if (!$newUserSuccess)
{
    AutoUserEntryPlugin::log('Couldn't save the new user: '.print_r($newUser->getErrors(), true), LogLevel::Info);
    throw new Exception(Craft::t('Couldnt save the user.'));
}

but in the log all that was output was "Couldn’t save the new user: Array"

How should I be doing this?

1 Answer 1

2

You're getting "Couldn’t save the new user: Array" because $newUser->getErrors() returns an array of validation errors (in case there are more than one).

If you want to print them all to a log file, you can do something like this:

AutoUserEntryPlugin::log('Couldn't save the new user: '.implode(', ', $newUser->getErrors()), LogLevel::Info);
1
  • If I use AutoUserEntryPlugin::log('Couldn’t save the new user: '.implode(', ', $newUser->getErrors()), LogLevel::Info); I get an error on that line; PHP notice Array to string conversion [edit: how do you get line breaks in these comments?!] Commented May 9, 2016 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.