Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
The one positive side of the API key issues recently is that it gave me
an opportunity to improve error handling 🔮
  • Loading branch information
zackkatz committed Jul 8, 2016
1 parent 15aa9cc commit a2486a2
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 32 deletions.
13 changes: 9 additions & 4 deletions admin/contacts.php
Expand Up @@ -214,6 +214,15 @@ private function generate_summary_report( $Contact ) {
protected function view() {


$params = kws_get_contacts_view_params();

$Contacts = $this->cc->getAllContacts( $params );

if( $Contacts instanceof CtctException ) {
$this->show_exception( $Contacts );
return;
}

kws_print_subsub( 'status', array(
array( 'val' => '', 'text' => __('Recently Updated', 'constant-contact-api') ),
array( 'val' => 'ACTIVE', 'text' => __('Active', 'constant-contact-api') ),
Expand All @@ -222,10 +231,6 @@ protected function view() {
array( 'val' => 'REMOVED', 'text' => __('Removed', 'constant-contact-api') ),
) );

$params = kws_get_contacts_view_params();

$Contacts = $this->cc->getAllContacts( $params );

/** @var Contact[] $Contacts Get them in chronological order */
$Contacts = array_reverse( $Contacts, true );
include( CTCT_DIR_PATH . 'views/admin/view.contacts-view.php' );
Expand Down
14 changes: 5 additions & 9 deletions admin/lists.php
@@ -1,11 +1,4 @@
<?php
use Ctct\ConstantContact;
use Ctct\Components\Contacts\Contact;
use Ctct\Components\Contacts\Address;
use Ctct\Components\Contacts\CustomField;
use Ctct\Components\Contacts\Note;
use Ctct\Components\Contacts\ContactList;
use Ctct\Components\Contacts\EmailAddress;
use Ctct\Exceptions\CtctException;


Expand Down Expand Up @@ -99,12 +92,15 @@ protected function view() {

$Lists = $this->cc->getAllLists();

if( $Lists instanceof CtctException ) {
$this->show_exception( $Lists );
return;
}

if ( empty( $Lists ) ) {
esc_html_e( 'Your account has no lists.', 'constant-contact-api' );
} else {

include( CTCT_DIR_PATH . 'views/admin/view.lists-view.php' );

}
}
}
Expand Down
5 changes: 4 additions & 1 deletion classes/class.ctct_admin_page.php
Expand Up @@ -219,7 +219,10 @@ protected function content() {
* @param \Ctct\Exceptions\CtctException $e
*/
protected function show_exception( $e ) {
echo '<div class="error inline"><h3>' . sprintf( esc_html__( 'There was an error displaying this content: %s', 'constant-contact-api' ), $e->getMessage() ) . '</h3></div>';

$error = KWSConstantContact::convertException( $e );

kws_print_notices( array( $error ), 'error inline', true, esc_html__( 'There was an error displaying this content:', 'constant-contact-api' ) );
}

public function help_tabs( $tabs ) {
Expand Down
50 changes: 41 additions & 9 deletions classes/class.kwsconstantcontact.php
Expand Up @@ -102,7 +102,7 @@ public function isConfigured( $force = false ) {
update_site_option( 'ctct_configured', 1 );
} catch ( CtctException $e ) {
$this->configured = 0;
do_action( 'ctct_debug', 'isConfigured: getContactByEmail failed. Deleting configured option.' );
do_action( 'ctct_error', 'isConfigured: getContactByEmail failed. Deleting configured option.' );
delete_site_option( 'ctct_configured' );
}

Expand Down Expand Up @@ -147,6 +147,7 @@ function getContact( $accessToken, $contactId ) {
try {
return $this->contactService->getContact( $accessToken, $contactId );
} catch ( Exception $e ) {
do_action( 'ctct_error', __METHOD__ . ': Exception', $e );
return $e;
}
}
Expand All @@ -165,6 +166,7 @@ public function updateContact( $accessToken, Contact $contact, $actionByContact
try {
return $this->contactService->updateContact( $accessToken, $contact, $actionByContact );
} catch ( Exception $e ) {
do_action( 'ctct_error', __METHOD__ . ': Exception', $e );
return $e;
}
}
Expand All @@ -184,6 +186,7 @@ public function getLists( $accessToken, Array $params = array() ) {
try {
return $this->listService->getLists( $accessToken, $params );
} catch ( CtctException $e ) {
do_action( 'ctct_error', __METHOD__ . ': Exception', $e );
return $e;
}
}
Expand All @@ -204,6 +207,7 @@ public function getList( $accessToken, $listId ) {

return $list;
} catch ( CtctException $e ) {
do_action( 'ctct_error', __METHOD__ . ': Exception', $e );
return $e;
}
}
Expand All @@ -220,6 +224,7 @@ public function updateList( $accessToken, $list ) {
try {
return $this->listService->updateList( $accessToken, $list );
} catch ( CtctException $e ) {
do_action( 'ctct_error', __METHOD__ . ': Exception', $e );
return $e;
}
}
Expand All @@ -241,6 +246,7 @@ public function getEmailCampaigns( $accessToken, Array $params = array() ) {
try {
return $this->emailMarketingService->getCampaigns( $accessToken, $params );
} catch ( CtctException $e ) {
do_action( 'ctct_error', __METHOD__ . ': Exception', $e );
return $e;
}
}
Expand All @@ -257,6 +263,7 @@ public function getEmailCampaign( $accessToken, $campaignId ) {
try {
return $this->emailMarketingService->getCampaign( $accessToken, $campaignId );
} catch ( CtctException $e ) {
do_action( 'ctct_error', __METHOD__ . ': Exception', $e );
return $e;
}
}
Expand All @@ -271,6 +278,7 @@ public function getEvents( $accessToken, Array $params = array() ) {
try {
return $this->eventService->getEvents( $accessToken, $params );
} catch ( CtctException $e ) {
do_action( 'ctct_error', __METHOD__ . ': Exception', $e );
return $e;
}
}
Expand All @@ -279,6 +287,7 @@ public function getEvent( $accessToken, $eventId ) {
try {
return $this->eventService->getEvent( $accessToken, $eventId );
} catch ( CtctException $e ) {
do_action( 'ctct_error', __METHOD__ . ': Exception', $e );
return $e;
}
}
Expand All @@ -287,6 +296,7 @@ public function updateEvent( $accessToken, EventSpot $event ) {
try {
return $this->eventService->updateEvent( $accessToken, $event );
} catch ( CtctException $e ) {
do_action( 'ctct_error', __METHOD__ . ': Exception', $e );
return $e;
}

Expand All @@ -296,6 +306,7 @@ public function getEventRegistrant( $accessToken, $eventId, $registrantId ) {
try {
return $this->eventService->getRegistrant( $accessToken, $eventId, $registrantId );
} catch ( CtctException $e ) {
do_action( 'ctct_error', __METHOD__ . ': Exception', $e );
return $e;
}
}
Expand All @@ -312,6 +323,7 @@ public function getEventRegistrants( $accessToken, $eventId, Array $params = arr
try {
return $this->eventService->getRegistrants( $accessToken, $id, $params );
} catch ( CtctException $e ) {
do_action( 'ctct_error', __METHOD__ . ': Exception', $e );
return $e;
}
}
Expand Down Expand Up @@ -422,16 +434,27 @@ public static function convertException( CtctException $exception ) {

preg_match( $regex, $error_message, $matches );

if ( ! empty( $matches['field'] ) ) {
$message_prefix .= ctct_get_label_from_field_id( $matches['field'] );
// CRUD error related to a user-generated request
if( ! empty( $matches ) ) {
if ( ! empty( $matches['field'] ) ) {
$message_prefix .= ctct_get_label_from_field_id( $matches['field'] );
}
if ( ! empty( $matches['subfield'] ) ) {
$message_prefix .= sprintf( ' (%s)', ctct_get_label_from_field_id( $matches['subfield'] ) );
}
$message = $matches['message'];
$wp_error_code = $matches[1];
}
if ( ! empty( $matches['subfield'] ) ) {
$message_prefix .= sprintf( ' (%s)', ctct_get_label_from_field_id( $matches['subfield'] ) );
// A service error
else {
$message_prefix = $code;
$message = $error_message;
$wp_error_code = $code;
}

$message = sprintf( '%s: %s', trim( $message_prefix ), trim( $matches['message'] ) );
$message = sprintf( '%s: %s', trim( $message_prefix ), trim( $message ) );

$wp_errors[] = new WP_Error( $matches[1], $message );
$wp_errors[] = new WP_Error( $wp_error_code, $message );
}

return ( 1 === sizeof( $wp_errors ) ) ? $wp_errors[0] : $wp_errors;
Expand Down Expand Up @@ -525,7 +548,7 @@ function getAllEventRegistrants( $id = NULL ) {
* @param array $passed_params Search filter. Sets the limit for requests.
* @param array $results Pass the previous results for recursive calls to the method.
*
* @return array Results array with `id` as key to each key/value pair.
* @return array|CtctException Results array with `id` as key to each key/value pair. If error, returns CtctException
*/
function getAll( $type = '', $passed_params = array(), &$results = array() ) {

Expand Down Expand Up @@ -590,13 +613,19 @@ function getAll( $type = '', $passed_params = array(), &$results = array() ) {
$fetch = $this->{"get{$type}"}( CTCT_ACCESS_TOKEN, $params );
$errors = ob_get_clean();

if( $fetch instanceof CtctException ) {
throw $fetch;
}

echo $errors;

if ( $cache_time && ! $fetch instanceof Exception ) {
set_transient( $cache_key, $fetch, $cache_time );
}
} catch ( CtctException $e ) {
$fetch = $e;
delete_transient( $cache_key );
do_action( 'ctct_error', 'Exception when getting all ' . $type, $e );
return $e;
}
}

Expand Down Expand Up @@ -678,6 +707,7 @@ public function getContactByEmail( $email, $null = NULL ) {

return false;
} catch ( CtctException $e ) {
do_action( 'ctct_error', __METHOD__ . ': Exception', $e );
return $e;
}
}
Expand Down Expand Up @@ -712,6 +742,7 @@ public function getContactsFromList( $accessToken, $list, $param = NULL ) {
try {
$listId = $this->getArgumentId( $list, 'ContactList' );
} catch ( CtctException $e ) {
do_action( 'ctct_error', __METHOD__ . ': Exception', $e );
return $e;
}
}
Expand All @@ -722,6 +753,7 @@ public function getContactsFromList( $accessToken, $list, $param = NULL ) {

return $contacts;
} catch ( CtctException $e ) {
do_action( 'ctct_error', __METHOD__ . ': Exception', $e );
return $e;
}
}
Expand Down
5 changes: 5 additions & 0 deletions classes/class.kwscontactlist.php
Expand Up @@ -146,6 +146,11 @@ static function outputHTML( $passed_items = array(), $atts = array() ) {
}
}

// There should always be at least one list.
if ( empty( $items ) || $items instanceof \Ctct\Exceptions\CtctException ) {
return sprintf( __('There was an error fetching lists. Please <a href="%s">refresh your lists</a> and try again.'), esc_url( admin_url( 'admin.php?page=constant-contact-lists&refresh=lists' ) ) );
}

$before = $before_item = $after_item = $after = $format = $id_attr = '';

switch ( $type ) {
Expand Down
4 changes: 2 additions & 2 deletions constant-contact-api.php
Expand Up @@ -4,7 +4,7 @@
Plugin URI: https://github.com/katzwebservices/Constant-Contact-WordPress-Plugin
Description: Powerfully integrate <a href="https://katz.si/6e" target="_blank">Constant Contact</a> into your WordPress website.
Author: Katz Web Services, Inc.
Version: 4.0.3
Version: 4.1
Author URI: https://katz.co
Text Domain: constant-contact-api
Domain Path: /languages
Expand All @@ -22,7 +22,7 @@

final class WP_CTCT {

const version = '4.0.3';
const version = '4.1';

/**
* @var string The minimum version of PHP required for the plugin
Expand Down
14 changes: 12 additions & 2 deletions inc/functions.php
Expand Up @@ -534,21 +534,31 @@ function kws_paginate_results( \Ctct\Components\ResultSet $resultSet, $limit = 5

/**
* Print an array of notices
*
* @since 4.0.3 Added $title parameter
*
* @param WP_Error[]|array $notices
* @param string $class
* @param bool $echo
* @param string $title If passed, used as the title of the error. Otherwise, taken from the error itself.
*
* @return string
*/
function kws_print_notices( $notices = array(), $class = 'updated', $echo = true ) {
function kws_print_notices( $notices = array(), $class = 'updated', $echo = true, $title = '' ) {

$output = '<div class="' . esc_attr( $class ) . '">';

foreach ( (array)$notices as $key => $notice ) {

if( is_wp_error( $notice ) ) {

$output .= '<h3>'.esc_html( sprintf( __('Error: %s', 'constant-contact-api'), $notice->get_error_code() ) ).'</h3>';
if( empty( $title ) ) {
$title = sprintf( __('Error: %s', 'constant-contact-api'), $notice->get_error_code() );
} else {
$title = sprintf( $title, $notice->get_error_code() );
}

$output .= '<h3>'.esc_html( $title ).'</h3>';

$output .= wpautop( esc_html( $notice->get_error_message() ) );

Expand Down
8 changes: 6 additions & 2 deletions lib/eventspot/eventspot.php
Expand Up @@ -5,6 +5,7 @@
*/

use \Ctct\Components\EventSpot\EventSpot;
use \Ctct\Exceptions\CtctException;

define('EVENTSPOT_FILE_PATH', dirname(__FILE__) . '/');
define('EVENTSPOT_FILE_URL', plugin_dir_url(__FILE__));
Expand Down Expand Up @@ -93,8 +94,11 @@ function print_scripts() {
function view() {

$events = $this->cc->getAll( 'Events' );

if(empty($events) || !is_array($events)) {

if( $events instanceof CtctException ) {
$this->show_exception( $events );
return;
} elseif(empty($events) || !is_array($events)) {
include( EVENTSPOT_FILE_PATH . '/views/promo.php' );
} else {

Expand Down
9 changes: 6 additions & 3 deletions readme.txt
Expand Up @@ -4,7 +4,7 @@ Donate link: http://wordpress.constantcontact.com
Tags: Constant Contact, Newsletter, Email Marketing, Mailing List, Newsletter, Events, Event Marketing
Requires at least: 3.3
Tested up to: 4.5.2
Stable tag: 4.0.3
Stable tag: 4.1
License: GPLv2 or later

Integrate Constant Contact into your website with this full-featured plugin.
Expand Down Expand Up @@ -77,17 +77,20 @@ To install the plugin follow the steps below:

== Changelog ==

= 4.0.4 on July 8, 2016 =
= 4.1 on July 8, 2016 =
* Updated: Improved error handling to show helpful messages
* Added: Additional error logging
* Fixed: Form Designer interface style issues
* Fixed: Error notices not printing properly
* Modified: `getAll()` methods `getAllLists()` `getAllContacts()` now returns `Ctct\Exceptions\CtctException` on error

= 4.0.3 on July 5, 2016 =

* Removed: Unused Select2 library, fixing an administration JavaScript error
* Fixed: Update admin JavaScript minification
* Updated: Plugin libraries

== 4.0.2 on June 29, 2016 =
= 4.0.2 on June 29, 2016 =

__This is a major update that requires PHP 5.5 or higher__. This was needed in order to use the latest Constant Contact code.

Expand Down

0 comments on commit a2486a2

Please sign in to comment.