libyui  3.10.0
YUIException.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YUIException.cc
20 
21  Stolen from zypp/libzypp/base/Exception.cc
22 
23  Author: Michael Andres <ma@suse.de>
24  Maintainer: Stefan Hundhammer <sh@suse.de>
25 
26 /-*/
27 
28 #include <sstream>
29 #include <string.h> // strerror()
30 #include <stdio.h>
31 
32 #define YUILogComponent "ui"
33 #include "YUILog.h"
34 
35 #include "YUIException.h"
36 #include "YWidget.h"
37 
38 using std::string;
39 using std::ostream;
40 
41 
43 {
44  string str( _file );
45  str += "(" + _func + "):";
46 
47  char formatted_number[ 20 ];
48  sprintf( formatted_number, "%u", _line );
49 
50  str += formatted_number;
51 
52  return str;
53 }
54 
55 
56 ostream &
57 operator<<( ostream & str, const YCodeLocation & obj )
58 {
59  return str << obj.asString();
60 }
61 
62 
64 {
65  // NOP
66 }
67 
68 YUIException::YUIException( const string & msg_r )
69  : _msg( msg_r )
70 {
71  // NOP
72 }
73 
74 
76 {
77  // NOP
78 }
79 
80 
81 string
83 {
84  std::ostringstream str;
85  dumpOn( str );
86  return str.str();
87 }
88 
89 
90 ostream &
91 YUIException::dumpOn( ostream & str ) const
92 {
93  return str << _msg;
94 }
95 
96 
97 ostream &
98 YUIException::dumpError( ostream & str ) const
99 {
100  return dumpOn( str << _where << ": " );
101 }
102 
103 
104 ostream &
105 operator<<( ostream & str, const YUIException & obj )
106 {
107  return obj.dumpError( str );
108 }
109 
110 
111 string
113 {
114  return strerror( errno_r );
115 }
116 
117 
118 string
119 YUIException::strErrno( int errno_r, const string & msg )
120 {
121  string ret( msg );
122  ret += ": ";
123  return ret += strErrno( errno_r );
124 }
125 
126 
127 void
128 YUIException::log( const YUIException & exception,
129  const YCodeLocation & location,
130  const char * const prefix )
131 {
132  YUILog::warning( YUILogComponent,
133  location.file().c_str(),
134  location.line(),
135  location.func().c_str() )
136  << "\t" << prefix << " " << exception.asString() << endl;
137 }
138 
139 
140 ostream &
142 {
143  if ( widget() )
144  {
145  return str << widget()->widgetClass()
146  << " has no property named \""
147  << property().name()
148  << "\""
149  << endl;
150  }
151  else
152  {
153  return str << "Unknown property name \""
154  << property().name()
155  << "\""
156  << endl;
157  }
158 }
159 
160 
161 ostream &
163 {
164  string widgetClass;
165 
166  if ( widget() )
167  widgetClass = string( widget()->widgetClass() ) + "::";
168 
169  return str << "Property type mismatch: "
170  << widgetClass
171  << property().name()
172  << " is type "
173  << property().typeAsStr()
174  << ", not "
175  << YProperty::typeAsStr( type() )
176  << endl;
177 }
178 
179 
180 ostream &
182 {
183  string widgetClass;
184 
185  if ( widget() )
186  widgetClass = string( widget()->widgetClass() ) + "::";
187 
188  return str << "Property "
189  << widgetClass
190  << property().name()
191  << "is read-only!"
192  << endl;
193 }
194 
195 
196 ostream &
198 {
199  string widgetClass;
200 
201  if ( widget() )
202  widgetClass = string( widget()->widgetClass() ) + "::";
203 
204  return str << "Bad argument for property "
205  << widgetClass
206  << property().name()
207  << ": "
208  << msg()
209  << endl;
210 }
YUIException::YUIException
YUIException()
Default constructor.
Definition: YUIException.cc:63
YCodeLocation::func
std::string func() const
Returns the name of the function where the exception occured.
Definition: YUIException.h:259
YUIPropertyTypeMismatchException::type
YPropertyType type() const
Return the property type the application tried to set.
Definition: YUIException.h:595
YUIException::~YUIException
virtual ~YUIException()
Destructor.
Definition: YUIException.cc:75
YUIPropertyException::widget
YWidget * widget() const
Returns the corresponding widget or 0 if there was none.
Definition: YUIException.h:528
YUIException::asString
std::string asString() const
Error message provided by dumpOn as string.
Definition: YUIException.cc:82
YUISetReadOnlyPropertyException::dumpOn
virtual std::ostream & dumpOn(std::ostream &str) const
Write proper error message with all relevant data.
Definition: YUIException.cc:181
YUIException::log
static void log(const YUIException &exception, const YCodeLocation &location, const char *const prefix)
Drop a log line on throw, catch or rethrow.
Definition: YUIException.cc:128
YUIPropertyException::property
YProperty property() const
Returns the property that caused this exception.
Definition: YUIException.h:523
YCodeLocation::file
std::string file() const
Returns the source file name where the exception occured.
Definition: YUIException.h:254
YCodeLocation::asString
std::string asString() const
Returns the location in normalized string format.
Definition: YUIException.cc:42
YProperty::typeAsStr
std::string typeAsStr() const
Returns the type of this property as string.
Definition: YProperty.h:82
YUIBadPropertyArgException::dumpOn
virtual std::ostream & dumpOn(std::ostream &str) const
Write proper error message with all relevant data.
Definition: YUIException.cc:197
YCodeLocation
Helper class for UI exceptions: Store FILE, FUNCTION and LINE.
Definition: YUIException.h:230
YWidget::widgetClass
virtual const char * widgetClass() const
Returns a descriptive name of this widget class for logging, debugging etc.
Definition: YWidget.h:72
YProperty::name
std::string name() const
Returns the name of this property.
Definition: YProperty.h:67
YUIPropertyTypeMismatchException::dumpOn
virtual std::ostream & dumpOn(std::ostream &str) const
Write proper error message with all relevant data.
Definition: YUIException.cc:162
YUIException::dumpOn
virtual std::ostream & dumpOn(std::ostream &str) const
Overload this to print a proper error message.
Definition: YUIException.cc:91
YUIUnknownPropertyException::dumpOn
virtual std::ostream & dumpOn(std::ostream &str) const
Write proper error message with all relevant data.
Definition: YUIException.cc:141
YUIException::strErrno
static std::string strErrno(int errno_r)
Make a string from errno_r.
Definition: YUIException.cc:112
YUIException::msg
const std::string & msg() const
Return the message string provided to the constructor.
Definition: YUIException.h:334
YCodeLocation::line
int line() const
Returns the source line number where the exception occured.
Definition: YUIException.h:264
YUIException
Base class for UI Exceptions.
Definition: YUIException.h:298