Tools

Class \Lib\Tools

Introduction

The \Lib\Tools class lets you access methods for error handling. This is the one you need to use to return a specific message with a selected error code, as well as a http code change.

Methods

errorException

<?php public static function \Lib\Tools::errorException($error_msg = '', $translate = false, $silent = true, $error_code = 0, $http_code = 400)

Description
Handles webservices' error using exceptions.

Parameters

  • error_msg
    The error message.
  • translate
    If this parameter is set to true, the error message will be translated. using the Apps Panel Text Manager and application language. (See section Text Manager for more details)
  • silent (optional)
    If this is set to true, it will force the error to be silent application side. (default true)
  • error_code (optional)
    The exception's error code. (default 0)
  • http_code (optional)
    The HTTP response status code. (default 400)

Return
new ApiException

formatError

<?php public static function \Lib\Tools::formatError(String $error_msg = '', Boolean $translate = false, Int $error_code = 0, Int $http_code = 200, Int $type_code = null)

Description
Handles errors without throwing exception.

Parameters

  • error_msg
    The error message.
  • translate
    If this parameter is set to true, the error message will be translated. using the Apps Panel Text Manager and application language. (See section Text Manager for more details)
  • error_code
    The error code (This code is relative to the application, it is up to you to define it according to your needs).
  • http_code
    The HTTP response's status code. DO NOT USE 5 codes** (Only used by the API Core) ! Return a 400 code if used.
  • type_code (optional)
    The error type (This code is relative to the application, it is up to you to define it according to your needs).

Return
An array representing the formatted error result to send.

{
  "error": {
    "message": "Id not found",
    "key": "id_not_found",
    "code": 1
  }
}

formatSuccess

<?php public static function \Lib\Tools::formatSuccess($success_msg = '', $translate = false, $http_code = 200)

Description
Handles webservices' success using the new formatted result.

Parameters

  • success_msg
    The success message.
  • translate (optional)
    If this parameter is set to true, the error message will be translated. using the Apps Panel Text Manager and application language. (See section Text Manager for more details)
  • http_code (optional)
    The HTTP response's status code. (default 200)

Return
Array : The formatted success result to send.

{
  "success": {
    "message": "Request was successful",
    "key": "activation_ok",
    "code": 200
  }
}

getAllHeaders

<?php public static function \Lib\Tools::getAllHeaders()

Description
Fetch all HTTP request headers (Alias for Apache getallheaders http://php.net/manual/fr/function.getallheaders.php)

Return
An associative array of all the HTTP headers in the current request, or FALSE on failure.


Examples

Verify that a username is passed as a GET parameter of a webservice

<?php

namespace App\Modules;

use \Lib\Controller\WSController;

/**
 * Class Users
 * @package App\Modules
 */
class Users extends \Modules\Elements
{

    /**
     * @param WSController $controller
     * @param array $params
     * @return array
     */
    public static function get(WSController $controller, $params = array())
    {
        if(empty($params['username'])) {
            return \Lib\Tools::formatError('username_missing', true, 1, 422, 100);
        }else{
          	//success
         	 	return \Lib\Tools::formatSuccess('username_existing');
        }
     
    }
}

The return would be :

<?php
return array(
  'error' => array(
    'message' => "The username is missing.", //Retrieved from the Text Manager
    'key'     => 101, //type_code + error_code
    'code'    => 1 //error_code
  )
);