The Battle for Wesnoth  1.19.0-dev
wml_exception.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2024
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  symbols["section"] = section;
82  symbols["key"] = key;
83  if(!primary_key.empty()) {
84  assert(!primary_value.empty());
85 
86  symbols["primary_key"] = primary_key;
87  symbols["primary_value"] = primary_value;
88 
89  return VGETTEXT("In section '[$section|]' where '$primary_key| = "
90  "$primary_value' the mandatory key '$key|' isn't set.", symbols);
91  } else {
92  return VGETTEXT("In section '[$section|]' the "
93  "mandatory key '$key|' isn't set.", symbols);
94  }
95 }
96 
98  const std::string &section
99  , const std::string &tag)
100 {
101  // TODO in 1.19: revisit this when we can add new strings
102  return missing_mandatory_wml_key(section, "[" + tag + "]");
103 }
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
static std::string _(const char *str)
Definition: gettext.hpp:93
Standard logging facilities (interface).
void line(int from_x, int from_y, int to_x, int to_y)
Draw a line.
Definition: draw.cpp:180
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:203
std::map< std::string, t_string > string_map
Helper class, don't construct this directly.
void show() const
Shows the error in a dialog.
std::string dev_message
The message for developers telling which problem was triggered, this shouldn't be translated.
std::string user_message
The message for the user explaining what went wrong.
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't call this directly.
static lg::log_domain log_engine("engine")
std::string missing_mandatory_wml_tag(const std::string &section, const std::string &tag)
Returns a standard message for a missing wml child (tag).
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 (attribute).
static lg::log_domain log_wml("wml")
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...