error

Vagrant – Errors trying to start

Using Vagrant as a dev environment is great apart from when it goes arry after an OS crash, or battery fault…

Chriss-MacBook-Pro:ceta chris$ vagrant reload

[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["hostonlyif", "create"]

Stderr: 0%...
Progress state: NS_ERROR_FAILURE
VBoxManage: error: Failed to create the host-only adapter
VBoxManage: error: VBoxNetAdpCtl: Error while adding new interface: failed to open /dev/vboxnetctl: No such file or directory

VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component HostNetworkInterface, interface IHostNetworkInterface
VBoxManage: error: Context: "int handleCreate(HandlerArg*, int, int*)" at line 68 of file VBoxManageHostonly.cpp

The only way I have found to fix this is to restart the VirtualBox service:

> sudo /Library/StartupItems/VirtualBox/VirtualBox restart

And then re-up it

> vagrant up

 

Read the last x lines of a log file

If like me you need to check out the PHP error log, but its bloody massive, you might want to take the last few lines of it.

tail -x log_file > new_log

That will take the last x number of lines from log_file and save into new_log

tail -10000 /var/log/httpd/error_log > ~/short_error_log

The above example will take the last 10,000 lines of the Apache error log, and save it into a new file ‘short_error_log’ in your home folder.

PHP Setting Error Reporting at runtime

You can use the PHP function error_reporting() at run time to set the level of error reporting in your application.

The different levels of reporting are based on bit masks or using a CONSTANT name. They are:

1       E_ERROR
2       E_WARNING
4       E_PARSE
8       E_NOTICE
16      E_CORE_ERROR
32      E_CORE_WARNING
64      E_COMPILE_ERROR
128     E_COMPILE_WARNING
256     E_USER_ERROR
512     E_USER_WARNING
1024    E_USER_NOTICE
2048    E_STRICT
4096    E_RECOVERABLE_ERROR
8192    E_DEPRECATED
16384   E_USER_DEPRECATED
32767   E_ALL

If you want to hide all errors : use zero:

error_reporting(0);

Otherwise, you can just use the bitmask or the name:

//show RECOVERABLE
error_reporting(4096);

//SHOW RECOVERABLE AND USER ERROR
error_reporting(4096 + 256);

//show warnings and deprecated
error_reporting(E_WARNING | E_DEPRECATED);

//show all except Notices
error_reporting(E_ALL ^ E_NOTICE);

PHP Create a ZIP file

I was just playing around with PHP’s ability to create compressed files, and I had a problem. My ZIP file was not getting created but I wasn’t getting any errors.

$zip = new ZipArchive();
$zipFile = '/tmp/' . uniqid() . '.zip';

if (file_exists($zipFile)) {
    unlink($zipFile);
}

$zip->open($zipFile, ZIPARCHIVE::CREATE);

foreach ($files as $type => $file) {
    $zip->addFile($file);
}

$zip->close();

Now, pay particular attention to the files you are adding using addFile() as if you have entered them wrong, you won’t get an error message, and infact, if you use $zip->getStatusString() you will receieve a “No error” message – completely throwing you off the scent.

More information about ZipArchive can be found at http://www.php.net/manual/en/class.ziparchive.php