/**
 *  @module     UtilityError
 *  @overview   Defines methods that parse Rocket in Pocket response and look for errors.
 *  
 *  @author     Animesh Mishra <hello@animesh.ltd>
 *  @copyright  © Animesh Ltd. All Rights Reserved.
 */

import { ExternalError } from "@magic.batua/error"

/** @exports UtilityError */
export var description = "Defines method that parse Rocket in Pocket response and look for errors."

/**
 *  Checks whether the given Rocket in Pocket API `response` has any errors. If not, returns
 *  `null`, otherwise returns an `ExternalError` describing the failure.
 *  
 *  @param {any} response   Response sent by Rocket in Pocket API
 *  
 *  @returns {ExternalError | null} 
 */
export function Check(response: any): ExternalError | null {
    if(response.message) { 
        return new ExternalError(response.message, "Rocket in Pocket", "RIP error code: " + response.errorCode) 
    }
    else {
        return null 
    }
}