libyui  3.3.1
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 
39 
40 
41 std::string YCodeLocation::asString() const
42 {
43  std::string str( _file );
44  str += "(" + _func + "):";
45 
46  char formatted_number[ 20 ];
47  sprintf( formatted_number, "%u", _line );
48 
49  str += formatted_number;
50 
51  return str;
52 }
53 
54 
55 std::ostream &
56 operator<<( std::ostream & str, const YCodeLocation & obj )
57 {
58  return str << obj.asString();
59 }
60 
61 
63 {
64  // NOP
65 }
66 
67 YUIException::YUIException( const std::string & msg_r )
68  : _msg( msg_r )
69 {
70  // NOP
71 }
72 
73 
75 {
76  // NOP
77 }
78 
79 
80 std::string
82 {
83  std::ostringstream str;
84  dumpOn( str );
85  return str.str();
86 }
87 
88 
89 std::ostream &
90 YUIException::dumpOn( std::ostream & str ) const
91 {
92  return str << _msg;
93 }
94 
95 
96 std::ostream &
97 YUIException::dumpError( std::ostream & str ) const
98 {
99  return dumpOn( str << _where << ": " );
100 }
101 
102 
103 std::ostream &
104 operator<<( std::ostream & str, const YUIException & obj )
105 {
106  return obj.dumpError( str );
107 }
108 
109 
110 std::string
112 {
113  return strerror( errno_r );
114 }
115 
116 
117 std::string
118 YUIException::strErrno( int errno_r, const std::string & msg )
119 {
120  std::string ret( msg );
121  ret += ": ";
122  return ret += strErrno( errno_r );
123 }
124 
125 
126 void
127 YUIException::log( const YUIException & exception,
128  const YCodeLocation & location,
129  const char * const prefix )
130 {
131  YUILog::warning( YUILogComponent,
132  location.file().c_str(),
133  location.line(),
134  location.func().c_str() )
135  << "\t" << prefix << " " << exception.asString() << std::endl;
136 }
137 
138 
139 std::ostream &
140 YUIUnknownPropertyException::dumpOn( std::ostream & str ) const
141 {
142  if ( widget() )
143  {
144  return str << widget()->widgetClass()
145  << " has no property named \""
146  << property().name()
147  << "\""
148  << std::endl;
149  }
150  else
151  {
152  return str << "Unknown property name \""
153  << property().name()
154  << "\""
155  << std::endl;
156  }
157 }
158 
159 
160 std::ostream &
161 YUIPropertyTypeMismatchException::dumpOn( std::ostream & str ) const
162 {
163  std::string widgetClass;
164 
165  if ( widget() )
166  widgetClass = std::string( widget()->widgetClass() ) + "::";
167 
168  return str << "Property type mismatch: "
169  << widgetClass
170  << property().name()
171  << " is type "
172  << property().typeAsStr()
173  << ", not "
174  << YProperty::typeAsStr( type() )
175  << std::endl;
176 }
177 
178 
179 std::ostream &
180 YUISetReadOnlyPropertyException::dumpOn( std::ostream & str ) const
181 {
182  std::string widgetClass;
183 
184  if ( widget() )
185  widgetClass = std::string( widget()->widgetClass() ) + "::";
186 
187  return str << "Property "
188  << widgetClass
189  << property().name()
190  << "is read-only!"
191  << std::endl;
192 }
193 
194 
195 std::ostream &
196 YUIBadPropertyArgException::dumpOn( std::ostream & str ) const
197 {
198  std::string widgetClass;
199 
200  if ( widget() )
201  widgetClass = std::string( widget()->widgetClass() ) + "::";
202 
203  return str << "Bad argument for property "
204  << widgetClass
205  << property().name()
206  << ": "
207  << msg()
208  << std::endl;
209 }
virtual ~YUIException()
Destructor.
Definition: YUIException.cc:74
virtual std::ostream & dumpOn(std::ostream &str) const
Write proper error message with all relevant data.
virtual std::ostream & dumpOn(std::ostream &str) const
Write proper error message with all relevant data.
int line() const
Returns the source line number where the exception occured.
Definition: YUIException.h:264
virtual std::ostream & dumpOn(std::ostream &str) const
Overload this to print a proper error message.
Definition: YUIException.cc:90
std::string asString() const
Error message provided by dumpOn as string.
Definition: YUIException.cc:81
std::string typeAsStr() const
Returns the type of this property as string.
Definition: YProperty.h:82
friend std::ostream & operator<<(std::ostream &str, const YCodeLocation &obj)
Stream output.
Definition: YUIException.cc:56
friend std::ostream & operator<<(std::ostream &str, const YUIException &obj)
YUIException stream output.
std::string func() const
Returns the name of the function where the exception occured.
Definition: YUIException.h:259
std::string file() const
Returns the source file name where the exception occured.
Definition: YUIException.h:254
YUIException()
Default constructor.
Definition: YUIException.cc:62
Helper class for UI exceptions: Store FILE, FUNCTION and LINE.
Definition: YUIException.h:229
const std::string & msg() const
Return the message string provided to the constructor.
Definition: YUIException.h:334
static void log(const YUIException &exception, const YCodeLocation &location, const char *const prefix)
Drop a log line on throw, catch or rethrow.
static std::string strErrno(int errno_r)
Make a string from errno_r.
std::string asString() const
Returns the location in normalized string format.
Definition: YUIException.cc:41
Base class for UI Exceptions.
Definition: YUIException.h:297
virtual std::ostream & dumpOn(std::ostream &str) const
Write proper error message with all relevant data.
virtual std::ostream & dumpOn(std::ostream &str) const
Write proper error message with all relevant data.