Skip to content

Commit

Permalink
Merge branch 'MDL-49747-task-error-improvement' of https://github.com…
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Apr 7, 2015
2 parents bfceaba + e8a1c3e commit e4d6ab3
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/classes/lock/lock.php
Expand Up @@ -49,6 +49,9 @@ class lock {
/** @var bool $released Has this lock been released? If a lock falls out of scope without being released - show a warning. */
protected $released;

/** @var string $caller Where was this called from? Stored for when a warning is shown */
protected $caller = 'unknown';

/**
* Construct a lock containing the unique key required to release it.
* @param mixed $key - The lock key. The type of this is up to the lock_factory being used.
Expand All @@ -59,6 +62,12 @@ public function __construct($key, $factory) {
$this->factory = $factory;
$this->key = $key;
$this->released = false;
$caller = debug_backtrace(true, 2)[1];
if ($caller && array_key_exists('file', $caller ) ) {
$this->caller = $caller['file'] . ' on line ' . $caller['line'];
} else if ($caller && array_key_exists('class', $caller)) {
$this->caller = $caller['class'] . $caller['type'] . $caller['function'];
}
}

/**
Expand Down Expand Up @@ -103,10 +112,14 @@ public function release() {
*/
public function __destruct() {
if (!$this->released && defined('PHPUNIT_TEST')) {
$key = $this->key;
$this->release();
throw new \coding_exception('\core\lock\lock(' . $this->key . ') has fallen out of scope ' .
'without being released.' . "\n" .
'Locks must ALWAYS be released by calling $mylock->release().');
throw new \coding_exception("A lock was created but not released at:\n" .
$this->caller . "\n\n" .
" Code should look like:\n\n" .
" \$factory = \core\lock\lock_config::get_lock_factory('type');\n" .
" \$lock = \$factory->get_lock($key);\n" .
" \$lock->release(); // Locks must ALWAYS be released like this.\n\n");
}
}

Expand Down

0 comments on commit e4d6ab3

Please sign in to comment.