The Battle for Wesnoth  1.15.1+dev
wml_exception.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Implementation for wml_exception.hpp.
18  */
19 
20 #define GETTEXT_DOMAIN "wesnoth-lib"
21 
22 #include "wml_exception.hpp"
23 
24 #include "gettext.hpp"
25 #include "gui/dialogs/message.hpp"
26 #include "formula/string_utils.hpp"
27 #include "log.hpp"
28 
29 static lg::log_domain log_engine("engine");
30 #define WRN_NG LOG_STREAM(warn, log_engine)
31 
33  const char* cond
34  , const char* file
35  , const int line
36  , const char *function
37  , const std::string& message
38  , const std::string& dev_message)
39 {
40  std::ostringstream sstr;
41  if(cond) {
42  sstr << "Condition '" << cond << "' failed at ";
43  } else {
44  sstr << "Unconditional failure at ";
45  }
46 
47  sstr << file << ":" << line << " in function '" << function << "'.";
48 
49  if(!dev_message.empty()) {
50  sstr << " Extra development information: " << dev_message;
51  }
52 
53  throw wml_exception(message, sstr.str());
54 }
55 
56 void wml_exception::show() const
57 {
58  std::ostringstream sstr;
59 
60  // The extra spaces between the \n are needed, otherwise the dialog doesn't show
61  // an empty line.
62  sstr << _("An error due to possibly invalid WML occurred\nThe error message is :")
63  << "\n" << user_message << "\n \n"
64  << _("When reporting the bug please include the following error message :")
65  << "\n" << dev_message;
66 
67  gui2::show_error_message(sstr.str());
68 }
69 
71  const std::string &section
72  , const std::string &key
73  , const std::string& primary_key
74  , const std::string& primary_value)
75 {
76  utils::string_map symbols;
77  if(!section.empty()) {
78  if(section[0] == '[') {
79  symbols["section"] = section;
80  } else {
81  WRN_NG << __func__
82  << " parameter 'section' should contain brackets."
83  << " Added them.\n";
84  symbols["section"] = "[" + section + "]";
85  }
86  }
87  symbols["key"] = key;
88  if(!primary_key.empty()) {
89  assert(!primary_value.empty());
90 
91  symbols["primary_key"] = primary_key;
92  symbols["primary_value"] = primary_value;
93 
94  return VGETTEXT("In section '[$section|]' where '$primary_key| = "
95  "$primary_value' the mandatory key '$key|' isn't set.", symbols);
96  } else {
97  return VGETTEXT("In section '[$section|]' the "
98  "mandatory key '$key|' isn't set.", symbols);
99  }
100 }
101 
103  const std::string& key
104  , const std::string& removal_version)
105 {
106  assert(!key.empty());
107  assert(!removal_version.empty());
108 
109  utils::string_map symbols;
110  symbols["key"] = key;
111  symbols["removal_version"] = removal_version;
112 
113  return VGETTEXT("The key '$key' is deprecated and support "
114  "will be removed in version $removal_version.", symbols);
115 }
116 
118  const std::string& deprecated_key
119  , const std::string& key
120  , const std::string& removal_version)
121 {
122  assert(!deprecated_key.empty());
123  assert(!key.empty());
124  assert(!removal_version.empty());
125 
126  utils::string_map symbols;
127  symbols["deprecated_key"] = deprecated_key;
128  symbols["key"] = key;
129  symbols["removal_version"] = removal_version;
130 
131  return VGETTEXT(
132  "The key '$deprecated_key' has been renamed to '$key'. "
133  "Support for '$deprecated_key' will be removed in version "
134  "$removal_version."
135  , symbols);
136 }
137 
139  const config& cfg
140  , const std::string& deprecated_key
141  , const std::string& key
142  , const std::string& removal_version)
143 {
144 
145  const config::attribute_value* result = cfg.get(key);
146  if(result) {
147  return *result;
148  }
149 
150  result = cfg.get(deprecated_key);
151  if(result) {
152  lg::wml_error()
154  deprecated_key
155  , key
156  , removal_version)
157  << '\n';
158 
159  return *result;
160  }
161 
162  static const config::attribute_value empty_attribute;
163  return empty_attribute;
164 }
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.
Variant for storing WML attributes.
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.
const attribute_value * get(config_key_type key) const
Returns a pointer to the attribute with the given key or nullptr if it does not exist.
Definition: config.cpp:742
const config::attribute_value & get_renamed_config_attribute(const config &cfg, const std::string &deprecated_key, const std::string &key, const std::string &removal_version)
Returns a config attribute, using either the old name or the new one.
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.
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
#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...
std::stringstream & wml_error()
Use this logger to send errors due to deprecated WML.
Definition: log.cpp:269
static lg::log_domain log_engine("engine")
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
std::string deprecated_renamed_wml_key_warning(const std::string &deprecated_key, const std::string &key, const std::string &removal_version)
Returns a standard warning message for using a deprecated renamed wml key.
static int cond(LexState *ls)
Definition: lparser.cpp:1177
Standard logging facilities (interface).
std::string deprecate_wml_key_warning(const std::string &key, const std::string &removal_version)
Returns a standard warning message for using a deprecated wml key.
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:205
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92