Skip to content

Commit

Permalink
Release 0.1.7 Better logging and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bossanova808 committed Aug 24, 2016
1 parent 05c0e76 commit d9fd6c1
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 19 deletions.
40 changes: 37 additions & 3 deletions multiadd/MultiAddPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,40 @@

class MultiAddPlugin extends BasePlugin
{
protected $settings;
static protected $settings;

/**
* Static log functions for this plugin
*
* @param mixed $msg
* @param string $level
* @param bool $force
*
* @return null
*/
public static function logError($msg){
MultiAddPlugin::log($msg, LogLevel::Error, $force = true);
}
public static function logWarning($msg){
MultiAddPlugin::log($msg, LogLevel::Warning, $force = true);
}
// If debugging is set to true in this plugin's settings, then log every message, devMode or not.
public static function log($msg, $level = LogLevel::Profile, $force = false)
{
if(self::$settings['debug']) $force=true;

if (is_string($msg))
{
$msg = "\n\n" . $msg . "\n";
}
else
{
$msg = "\n\n" . print_r($msg, true) . "\n";
}

parent::log($msg, $level, $force);
}


public function init()
{
Expand All @@ -21,7 +54,7 @@ public function getName()

public function getVersion()
{
return '0.1.6';
return '0.1.7';
}

public function getSchemaVersion()
Expand Down Expand Up @@ -62,7 +95,8 @@ public function hasSettings()
public function defineSettings()
{
return array(
'debug' => AttributeType::Bool,
'debug' => AttributeType::Bool,
'debugPOST' => AttributeType::Bool,
);
}

Expand Down
19 changes: 7 additions & 12 deletions multiadd/controllers/MultiAddController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ class MultiAddController extends Commerce_BaseFrontEndController

protected $allowAnonymous = true;

/**
* @param $error
*/
private function logError($error){
MultiAddPlugin::log($error, LogLevel::Error);
}


/**
* @throws HttpException
*/
Expand All @@ -26,7 +18,7 @@ public function actionMultiAdd()
//Get plugin settings
$settings = craft()->plugins->getPlugin('multiAdd')->getSettings();
//Settings to control behavour when testing - we don't want to debug via ajax or it stuffs up the JSON response...
$debug = ($settings->debug and !$ajax);
$debug = ($settings->debugPOST and !$ajax);

//Store items added to the cart in case of later failure & rollback required
$rollback = array();
Expand Down Expand Up @@ -60,7 +52,7 @@ public function actionMultiAdd()
break;
}
}
if(!$itemsToProcess){
if(!$itemsToProcess){
$errors[] = "All items have 0 quantity.";
}
}
Expand All @@ -76,8 +68,10 @@ public function actionMultiAdd()

//trouble?
if ($errors) {
foreach ($errors as $error) {
$this->logError($error);
//Try to log referrer in case of misadventure - might help track down odd errors
MultiAddPlugin::logError(craft()->request->getUrlReferrer());
foreach ($errors as $error) {
MultiAddPlugin::logError($error);
}
craft()->urlManager->setRouteVariables(['error' => $errors]);
}
Expand Down Expand Up @@ -130,6 +124,7 @@ public function actionUpdateCart()
craft()->userSession->setError(Craft::t('Couldn’t update line item: {message}', [
'message' => $error
]));
MultiAddPlugin::logError('Couldn’t update line item: [$error]');
} else {
craft()->userSession->setNotice(Craft::t('Items updated.'));
$this->redirectToPostedUrl();
Expand Down
17 changes: 14 additions & 3 deletions multiadd/services/MultiAdd_CartService.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ public function multiAddToCart($order, $items, &$error = '')
//saving current cart if it's new and empty
if (!$order->id) {
if (!craft()->commerce_orders->saveOrder($order)) {
throw new Exception(Craft::t('Error on creating empty cart'));
CommerceDbHelper::rollbackStackedTransaction();
MultiAddPlugin::logError('Error on creating empty cart');
throw new Exception(Craft::t('Error on creating empty cart in multiadd'));
}
}

Expand Down Expand Up @@ -158,8 +160,15 @@ public function multiAddToCart($order, $items, &$error = '')
}

if($success){
craft()->commerce_orders->saveOrder($order);
CommerceDbHelper::commitStackedTransaction();
$orderSaveSuccess = craft()->commerce_orders->saveOrder($order);
if($orderSaveSuccess) {
CommerceDbHelper::commitStackedTransaction();
}
else{
MultiAddPlugin::logError('Error when saving order');
CommerceDbHelper::rollbackStackedTransaction();
throw new Exception(Craft::t('Error saving order in multiadd'));
}

//raising event
$event = new Event($this, [
Expand All @@ -171,6 +180,8 @@ public function multiAddToCart($order, $items, &$error = '')
return true;
}
else{
MultiAddPlugin::logError('Error');
MultiAddPlugin::logError($lineItem->getAllErrors());
CommerceDbHelper::rollbackStackedTransaction();
$errors = $lineItem->getAllErrors();
$error = array_pop($errors);
Expand Down
17 changes: 16 additions & 1 deletion multiadd/templates/_settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ <h4>{{ 'Debugging?' | t }}</h4>
on: settings.debug,
}) }}

<p>{{ "Dumps the POST data back to the page &amp; prevents redirects, so you can see if your form is submitting the correct data. (This setting is ignored if you're submitting by ajax.)" | t }}</p>
<p>Makes <code>LogLevel::Profile</code> log messages get logged.</p>
<p><code>LogLevel::Warning</code> and <code>LogLevel::Error</code> are always logged.</p>

<br>

<h4>{{ 'Debug POST?' | t }}</h4>

{{ forms.lightswitch({
onLabel: "On",
offLabel: "Off",
name: "debugPOST",
on: settings.debugPOST,
}) }}

<p>{{ "Dumps the POST data back to the page & prevents redirects, so you can see if your form is submitting the correct data. (This setting is ignored if you're submitting by ajax - use your developer console to check that!)" | t }}</p>

</div>
{% endblock %}
8 changes: 8 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
[
{
"version": "0.1.7",
"downloadUrl": "https://github.com/engram-design/MultiAdd/archive/0.1.7.zip",
"date": "2016-08-24T11:17:28+10:00",
"notes": [
"[Fixed] Added more logging/rollback of transactions if various errors occur - help with possible rare race condition?",
"[Added] Debugging swtich now controls log of all log messages, debug POST offers previous debugging behaviour"
]
}, {
"version": "0.1.6",
"downloadUrl": "https://github.com/engram-design/MultiAdd/archive/0.1.6.zip",
"date": "2016-08-16T11:17:28+10:00",
Expand Down

0 comments on commit d9fd6c1

Please sign in to comment.