Debugging

SDK raises errors using exceptions:

Debug mode

SDK allows to activate debug mode. Its purpose is to log all requests, responses and headers.

The debugging may be enabled by defining DREAMCOMMERCE_DEBUG constant.

value meaning
false debug mode is disabled
true debug mode is enabled

You can define the constant in your source code:

define('DREAMCOMMERCE_DEBUG', true);

Logging messages

SDK allows to log all messages to stream.

The log file may be set by defining DREAMCOMMERCE_LOG_FILE constant.

value meaning
false logging is disabled
string file path or stream (i.e. "php://stdout", "logs/application.log")

You can define the constant in your source code:

define('DREAMCOMMERCE_LOG_FILE', "php://stdout");

Messages can be passed to simple logger class using multiple priorities:

\DreamCommerce\Logger::debug("debug message");
\DreamCommerce\Logger::info("informational message");
\DreamCommerce\Logger::notice("notice message");
\DreamCommerce\Logger::warning("warning message");
\DreamCommerce\Logger::error("error message");
\DreamCommerce\Logger::critical("critical message");
\DreamCommerce\Logger::alert("alert message");
\DreamCommerce\Logger::emergency("emergency message");

Debug messages are filtered if debug mode is disabled.

Catching exceptions

A code example using exceptions handling:

try{
    $client = new \DreamCommerce\Client(
        'https://myshop.example.com', 'Application ID', 'Secret'
    );

    $client->setAccessToken('SHOP TOKEN');

    // fetch collection/object
    $product = new \DreamCommerce\Resource\Product($client);
    $list = $product->get();

    foreach($list as $item){
        //...
    }

} catch (\DreamCommerce\Exception\ClientException $ex) {
    // client error
    \DreamCommerce\Logger::error($ex);
} catch (\DreamCommerce\Exception\ResourceException $ex) {
    // resource error
    \DreamCommerce\Logger::error($ex);
}

Each exception lets to access an exception of lower layer, eg. HTTP response. Simply use standard exception’s method getPrevious on every exception.

try{

    // ...

} catch (\DreamCommerce\Exception\ClientException $ex) {
    \DreamCommerce\Logger::error(sprintf("Client error: %s", $ex->getMessage()));

    $prev = $ex->getPrevious();

    if($prev instanceof \DreamCommerce\Exception\HttpException){
        \DreamCommerce\Logger::error(sprintf("HTTP error: %s", $prev->getMessage()));

        if($prev->getCode() == \DreamCommerce\Exception\HttpException::QUOTA_EXCEEDED){
            \DreamCommerce\Logger::warning("Quota exceeded");
        }
    }

} catch (\DreamCommerce\Exception\ResourceException $ex) {
    \DreamCommerce\Logger::error(sprintf("Resource error: %s", $ex->getMessage()));
}

In order to directly access error message, it’s possible to use static method DreamCommerce\Client::getError.

try{

    // ...

} catch (\DreamCommerce\Exception\ClientException $ex) {
    \DreamCommerce\Logger::error(sprintf("Client error: %s", Client::getError($ex)));
} catch (\DreamCommerce\Exception\ResourceException $ex) {
    \DreamCommerce\Logger::error(sprintf("Resource error: %s", Client::getError($ex)));
}