The library contains the following functions:
- TradeServerReturnCodeDescription – returns description of trade server return codes;
- ErrorDescription – returns description of runtime errors.
//+------------------------------------------------------------------+ //| ErrorDescription.mqh | //| Copyright 2010, MetaQuotes Software Corp. | //| | //+------------------------------------------------------------------+ #property copyright "2010, MetaQuotes Software Corp." #property link "" #property version "1.00" //+------------------------------------------------------------------+ //| returns trade server return code description | //+------------------------------------------------------------------+ string TradeServerReturnCodeDescription(int return_code) //+------------------------------------------------------------------+ //| returns runtime error code description | //+------------------------------------------------------------------+ string ErrorDescription(int err_code)
Example:
(Don’t forget to copy the file ErrorDescription.mq5 to the folder \MetaTrader 5\MQL5\Include)
//+------------------------------------------------------------------+ //| ErrorDescrTest.mq5 | //| Copyright 2010, MetaQuotes Software Corp. | //| | //+------------------------------------------------------------------+ #property copyright "2010, MetaQuotes Software Corp." #property link "" #property version "1.00" #include <ErrorDescription.mqh> //+------------------------------------------------------------------+ //| Example of use of the ErrorDescription.mqh library | //+------------------------------------------------------------------+ void OnStart() { Print("----- Description of trade server return codes -----"); for(int i=10004;i<=10034;i++) { Print("Trade server return code:",i,TradeServerReturnCodeDescription(i)); } Print("-------- Description of runtime error codes ---------"); for(int i=4001;i<=4014;i++) { Print("Runtime error code:",i,ErrorDescription(i)); } } //+------------------------------------------------------------------+
In some cases it’s necessary to work with user defined errors. In MQL5 there is a function SetUserError, what sets the predefined variable _LastError into the value equal to ERR_USER_ERROR_FIRST + user_error.
The user defined error codes starts from code ERR_USER_ERROR_FIRST. In such cases you can use the function ErrorDescriptionExt to return description of errors, including the user defined errors:
//+------------------------------------------------------------------+ //| UserErrorDescr.mq5 | //| Copyright 2010, MetaQuotes Software Corp. | //| | //+------------------------------------------------------------------+ #property copyright "2010, MetaQuotes Software Corp." #property link "" #property version "1.00" #include <ErrorDescription.mqh> //+------------------------------------------------------------------+ //| returns runtime error code description, | //| with user defined errors | //+------------------------------------------------------------------+ string ErrorDescriptionExt(int err_code,string&user_errors[]) { if(err_code>=0 && err_code<ERR_USER_ERROR_FIRST) return(ErrorDescription(err_code)); //--- user defined runtime errors err_code-=ERR_USER_ERROR_FIRST; if(err_code<=ArraySize(user_errors)) return(user_errors[err_code]); //--- return("Unknown error"); }; // an array with description of the user defined runtime errors string MyErrors[]= { "User error №1", "User error №2", "User error №3" }; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- for(int i=0;i<=2;i++) { SetUserError(i); Print("User defined error code:",i,ErrorDescriptionExt(GetLastError(),MyErrors)); } } //+------------------------------------------------------------------+