//+------------------------------------------------------------------+ //| LibError_HowTo.mq5 | //| Copyright 2020, Freie Netze UG (haftunbgsbeschränkt) | //| https://www.freie-netze.de/ | //+------------------------------------------------------------------+ #property service #property copyright "Copyright 2020, Freie Netze UG (haftunbgsbeschränkt)" #property link "https://www.freie-netze.de/" #property version "1.00" ///////////////////////////////////////////////////////////////////////////////////////////////////// // // Setup Lib Error // // Configure for shared library include // If this option is not set, the library will be included as header //#define LIB_ERROR_IMPORT_SHARED_LIBRARY // User supplied OnError handler // Edit your own OnError handler, see file lib_error.mqh #define LIB_ERR_USER_ERROR_HANDLER // Generate unified function macro substitution // If this option is not set, all function calls need to be specified with the namespace LibError:: #define LIB_ERROR_API_FUNCTION_SHADOWING // Handle warning error codes as errors //#define LIB_ERR_LEVEL_WARNING_AS_ERROR // ///////////////////////////////////////////////////////////////////////////////////////////////////// #include /* ********************************************************************************************************* * Provider return codes * ********************************************************************************************************* */ ////////////////////////////// // // Define error group id // // Group IDs span from 0x01 to 0xEF // Group IDs from 0xF0 to 0xFF are considered reserved // #define EA_ERRGROUP_CODE_EA_CORE 0x01 ////////////////////////////// // // Defined return codes // // Error codes span from 0x00 which is always a SUCCESS-Code // to 0xDF which are considered errors. // // The range 0xE0 to 0xEF is considered warnings // And the range 0xF0 to 0xFF is considered handled errors. // // The way these codes are interpeted can be changed by // defininng your own OnError handler, which is located in // the file "lib_error.mqh" and called UserOnError() // // Feel free to adapt as you like. // #define CORE_SUCCESS LIB_ERROR_CODE(EA_ERRGROUP_CODE_EA_CORE, 0x00) #define CORE_ERR_OPERATION_FAILED LIB_ERROR_CODE(EA_ERRGROUP_CODE_EA_CORE, 0x01) #define CORE_ERR_MISSING_PARAMETER LIB_ERROR_CODE(EA_ERRGROUP_CODE_EA_CORE, 0x02) #define CORE_INFO_CORE_READY LIB_ERROR_CODE_HANDLED(EA_ERRGROUP_CODE_EA_CORE, 0x01) #define CORE_WARN_PENDING_OPERATION LIB_ERROR_CODE_WARNING(EA_ERRGROUP_CODE_EA_CORE, 0x01) #define CORE_WARN_OPEN_POSITION LIB_ERROR_CODE_WARNING(EA_ERRGROUP_CODE_EA_CORE, 0x02) // Define message resolver LIB_ERR_REGISTER_RESOLVER_BEGIN(EA_ERRGROUP_CODE_EA_CORE) LIB_ERR_REGISTER_RESOLVER_MSGCODE(CORE_SUCCESS, "CORE_SUCCESS", "Operation success. An order has been processed.") LIB_ERR_REGISTER_RESOLVER_MSGCODE(CORE_INFO_CORE_READY, "CORE_INFO_CORE_READY", "Expert advisor: Core ready.") LIB_ERR_REGISTER_RESOLVER_MSGCODE(CORE_WARN_PENDING_OPERATION, "CORE_WARN_PENDING_OPERATION", "A pending operation is awaiting confirmation.") LIB_ERR_REGISTER_RESOLVER_MSGCODE(CORE_WARN_OPEN_POSITION, "CORE_WARN_OPEN_POSITION", "An open position is left unmanaged.") LIB_ERR_REGISTER_RESOLVER_MSGCODE(CORE_ERR_OPERATION_FAILED, "CORE_ERR_OPERATION_FAILED", "Operation has failed.") LIB_ERR_REGISTER_RESOLVER_MSGCODE(CORE_ERR_MISSING_PARAMETER, "CORE_ERR_MISSING_PARAMETER", "Parameter missing.") LIB_ERR_REGISTER_RESOLVER_END(EA_ERRGROUP_CODE_EA_CORE) //+------------------------------------------------------------------+ //| Service program start function | //+------------------------------------------------------------------+ void OnStart() { //--- /////////////////////////////////////////////////////////////////////////////////////////////////////////// // // List of availabe functions // err_SetUserError(CORE_ERR_MISSING_PARAMETER, "Some comment on this error"); OnError(); // This OnError() call is for demonstration purpose. // Function signature: // bool err_SetUserError( const uint err_code, // const string comment = "", const string comment1 = "", const string comment2 = "", const string comment3 = "", const string comment4 = "", const string comment5 = "", // const string comment6 = "", const string comment7 = "", const string comment8 = "", const string comment9 = "", const string commentA = "", const string commentB = "", // const string commentC = "", const string commentD = "", const string commentE = "", const string commentF = ""); // // Return value: // Returns true if the error code set is actually a valid error code. // err_ResetLastError(); // Function signature: // void err_ResetLastError(); // err_GetLastError(); // Function signature: // int err_GetLastError(); // // Return value: // Returns the last error code, clears also the last error code. // Subsequent queries on the last occured error can be received by GetLastResolvedError. // SetMqlError(4022, "Setting any MQL API error code"); // Function signature: // void SetMqlErrorCode( const uint err_code, // const string comment = "", const string comment1 = "", const string comment2 = "", const string comment3 = "", const string comment4 = "", const string comment5 = "", // const string comment6 = "", const string comment7 = "", const string comment8 = "", const string comment9 = "", const string commentA = "", const string commentB = "", // const string commentC = "", const string commentD = "", const string commentE = "", const string commentF = ""); // ResetUserError(); // Function signature: // void ResetUserError(); // ResolveLastError(); // Function signature: // string ResolveLastError(); // string ResolveLastError( int& error_code); // int ResolveLastError( string& error_msg); // string ResolveLastError( const int error_code, // const string prefix); // // Return value: // Returns the actual error message, including any prefix given. // GetLastResolvedError(); // Function signature: // int GetLastResolvedError(); // // Return value: // Returns the last occured error code, even if this has already been // cleard by ResolveLastError or ResetLastError or ResetUserError. // OnError(); // Function signature: // bool OnError( const string prefix = NULL) // bool OnError( const int line_no) // bool OnError( int& err, // const string prefix = NULL) // // Return value: // True if an error code has been registered by a function or by SetMqlError or SetUserError // // Note: // This function can be altered at free will to behave as your needs require. // To do so, edit the body of the function UserOnError in file: lib_error.mqh (Located in shared project "MQLplus") // /////////////////////////////////////////////////////////////////////////////////////////////////////////// // // Function shadowing // // These functions are equivalent to err_* functions from above list. // This feature is only available, if function shadowing has been enabled. // SetUserError(CORE_WARN_PENDING_OPERATION); // -> err_SetUserError(); ResetLastError(); // -> err_ResetLastError(); GetLastError(); // -> err_GetLastError(); } //+------------------------------------------------------------------+