The Battle for Wesnoth  1.17.12+dev
wml_exception.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2022
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  * Implementation for wml_exception.hpp.
19  */
20 
21 #define GETTEXT_DOMAIN "wesnoth-lib"
22 
23 #include "wml_exception.hpp"
24 
25 #include "gettext.hpp"
26 #include "gui/dialogs/message.hpp"
27 #include "formula/string_utils.hpp"
28 #include "log.hpp"
29 
30 static lg::log_domain log_engine("engine");
31 #define WRN_NG LOG_STREAM(warn, log_engine)
32 
33 static lg::log_domain log_wml("wml");
34 #define ERR_WML LOG_STREAM(err, log_wml)
35 
37  const char* cond
38  , const char* file
39  , const int line
40  , const char *function
41  , const std::string& message
42  , const std::string& dev_message)
43 {
44  std::ostringstream sstr;
45  if(cond) {
46  sstr << "Condition '" << cond << "' failed at ";
47  } else {
48  sstr << "Unconditional failure at ";
49  }
50 
51  sstr << file << ":" << line << " in function '" << function << "'.";
52 
53  if(!dev_message.empty()) {
54  sstr << " Extra development information: " << dev_message;
55  }
56 
57  throw wml_exception(message, sstr.str());
58 }
59 
60 void wml_exception::show() const
61 {
62  std::ostringstream sstr;
63 
64  // The extra spaces between the \n are needed, otherwise the dialog doesn't show
65  // an empty line.
66  sstr << _("An error due to possibly invalid WML occurred\nThe error message is :")
67  << "\n" << user_message << "\n \n"
68  << _("When reporting the bug please include the following error message :")
69  << "\n" << dev_message;
70 
71  gui2::show_error_message(sstr.str());
72 }
73 
75  const std::string &section
76  , const std::string &key
77  , const std::string& primary_key
78  , const std::string& primary_value)
79 {
80  utils::string_map symbols;
81  if(!section.empty()) {
82  if(section[0] == '[') {
83  symbols["section"] = section;
84  } else {
85  WRN_NG << __func__
86  << " parameter 'section' should contain brackets."
87  << " Added them.";
88  symbols["section"] = "[" + section + "]";
89  }
90  }
91  symbols["key"] = key;
92  if(!primary_key.empty()) {
93  assert(!primary_value.empty());
94 
95  symbols["primary_key"] = primary_key;
96  symbols["primary_value"] = primary_value;
97 
98  return VGETTEXT("In section '[$section|]' where '$primary_key| = "
99  "$primary_value' the mandatory key '$key|' isn't set.", symbols);
100  } else {
101  return VGETTEXT("In section '[$section|]' the "
102  "mandatory key '$key|' isn't set.", symbols);
103  }
104 }
std::map< std::string, t_string > string_map
void throw_wml_exception(const char *cond, const char *file, const int line, const char *function, const std::string &message, const std::string &dev_message)
Helper function, don&#39;t call this directly.
Add a special kind of assert to validate whether the input from WML doesn&#39;t contain any problems that...
std::string user_message
The message for the user explaining what went wrong.
static std::string _(const char *str)
Definition: gettext.hpp:93
std::string missing_mandatory_wml_key(const std::string &section, const std::string &key, const std::string &primary_key, const std::string &primary_value)
Returns a standard message for a missing wml key.
void show() const
Shows the error in a dialog.
#define WRN_NG
Helper class, don&#39;t construct this directly.
std::string dev_message
The message for developers telling which problem was triggered, this shouldn&#39;t be translated...
void line(int from_x, int from_y, int to_x, int to_y)
Draw a line.
Definition: draw.cpp:171
static lg::log_domain log_engine("engine")
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
Standard logging facilities (interface).
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:204
static lg::log_domain log_wml("wml")