KiCad PCB EDA Suite
Loading...
Searching...
No Matches
exceptions.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2007-2016 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <wx/string.h>
22#include <wx/translation.h>
23#include <ki_exception.h>
24
25
26const wxString IO_ERROR::What() const
27{
28#ifdef DEBUG
29 return wxString( wxS( "IO_ERROR: " ) ) + Problem() + wxS("\n\n" ) + Where();
30#else
31 return Problem();
32#endif
33}
34
35
36const wxString IO_ERROR::Where() const
37{
38 return where;
39}
40
41
42const wxString IO_ERROR::Problem() const
43{
44 return problem;
45}
46
47
48void IO_ERROR::init( const wxString& aProblem, const char* aThrowersFile,
49 const char* aThrowersFunction, int aThrowersLineNumber )
50{
51 problem = aProblem;
52
53 // The throwers filename is a full filename, depending on KiCad source location.
54 // a short filename will be printed (it is better for user, the full filename has no meaning).
55 wxString srcname = aThrowersFile;
56
57 // No need for translations of source code file/line messages (and wxWidget's translation
58 // stuff has bitten us before. May be related to KICAD-YP.
59 where.Printf( wxS( "from %s : %s() line %d" ),
60 srcname.AfterLast( '/' ),
61 wxString( aThrowersFunction ),
62 aThrowersLineNumber );
63
64 updateWhat();
65}
66
67
68const char* IO_ERROR::what() const throw()
69{
70 return whatMessage.c_str();
71}
72
73
75{
76 whatMessage = problem.utf8_string();
77}
78
79
80IO_CANCELLED::IO_CANCELLED( const char* aThrowersFile, const char* aThrowersFunction, int aThrowersLineNumber,
81 const wxString& aProblem ) :
82 IO_ERROR( aProblem.IsEmpty() ? _( "Canceled by user." ) : aProblem, aThrowersFile, aThrowersFunction,
83 aThrowersLineNumber )
84{
85}
86
87
88void PARSE_ERROR::init( const wxString& aProblem, const char* aThrowersFile,
89 const char* aThrowersFunction, int aThrowersLineNumber,
90 const wxString& aSource, const char* aInputLine, int aLineNumber,
91 int aByteIndex )
92{
93 parseProblem = aProblem;
94
95 problem.Printf( _( "%s in '%s', line %d, offset %d." ),
96 aProblem,
97 aSource,
98 aLineNumber,
99 aByteIndex );
100
101 inputLine = aInputLine;
102 lineNumber = aLineNumber;
103 byteIndex = aByteIndex;
104
105 // The throwers filename is a full filename, depending on KiCad source location.
106 // a short filename will be printed (it is better for user, the full filename has no meaning).
107 wxString srcname = aThrowersFile;
108
109 // No need for translations of source code file/line messages (and wxWidget's translation
110 // stuff has bitten us before. May be related to KICAD-YP.
111 where.Printf( wxS( "from %s : %s() line %d" ),
112 srcname.AfterLast( '/' ),
113 wxString( aThrowersFunction ),
114 aThrowersLineNumber );
115
116 updateWhat();
117}
118
119
120void FUTURE_FORMAT_ERROR::init( const wxString& aRequiredVersion,
121 const wxString& aRequiredGenerator )
122{
123 requiredVersion = aRequiredVersion;
124 requiredGenerator = aRequiredGenerator;
125
126 if( requiredGenerator.IsEmpty() )
127 {
128 problem.Printf( _( "KiCad was unable to open this file because it was created with a more "
129 "recent version than the one you are running.\n\n"
130 "To open it you will need to upgrade KiCad to a version dated %s or "
131 "later." ),
132 aRequiredVersion );
133 }
134 else
135 {
136 problem.Printf( _( "KiCad was unable to open this file because it was created with a more "
137 "recent version than the one you are running.\n\n"
138 "To open it you will need to upgrade KiCad to version %s or "
139 "later (file format dated %s or later)." ),
140 aRequiredGenerator, aRequiredVersion );
141 }
142
143 updateWhat();
144}
145
146
147FUTURE_FORMAT_ERROR::FUTURE_FORMAT_ERROR( const wxString& aRequiredVersion,
148 const wxString& aRequiredGenerator ) :
150{
151 init( aRequiredVersion, aRequiredGenerator );
152
153 lineNumber = 0;
154 byteIndex = 0;
155}
156
157
159 const wxString& aRequiredVersion,
160 const wxString& aRequiredGenerator ) :
162{
163 if( const FUTURE_FORMAT_ERROR* ffe = dynamic_cast<const FUTURE_FORMAT_ERROR*>( &aParseError ) )
164 {
165 requiredVersion = ffe->requiredVersion;
166 requiredGenerator = ffe->requiredGenerator;
167 problem = ffe->Problem();
168 }
169 else
170 {
171 init( aRequiredVersion, aRequiredGenerator );
172
173 if( !aParseError.Problem().IsEmpty() )
174 problem += wxS( "\n\n" ) + _( "Full error text:" ) + wxS( "\n" ) +
175 aParseError.Problem();
176 }
177
178 lineNumber = aParseError.lineNumber;
179 byteIndex = aParseError.byteIndex;
180 inputLine = aParseError.inputLine;
181 updateWhat();
182}
IO_CANCELLED(const char *aThrowersFile, const char *aThrowersFunction, int aThrowersLineNumber, const wxString &aProblem=wxEmptyString)
Normally thrown via the macro THROW_IO_CANCELLED(), which supplies the call-site file,...
void init(const wxString &aProblem, const char *aThrowersFile, const char *aThrowersFunction, int aThrowersLineNumber)
virtual const wxString What() const
A composite of Problem() and Where()
IO_ERROR(const wxString &aProblem, const char *aThrowersFile, const char *aThrowersFunction, int aThrowersLineNumber)
Use macro THROW_IO_ERROR() to wrap a call to this constructor at the call site.
wxString problem
void updateWhat()
virtual const wxString Problem() const
what was the problem?
wxString where
std::string whatMessage
virtual const char * what() const override
std::exception interface, returned as UTF-8
virtual const wxString Where() const
where did the Problem() occur?
#define _(s)
wxString requiredGenerator
Version of KiCad required to open file.
FUTURE_FORMAT_ERROR(const wxString &aRequiredVersion, const wxString &aRequiredGenerator=wxEmptyString)
void init(const wxString &aRequiredVersion, const wxString &aRequiredGenerator=wxEmptyString)
wxString requiredVersion
Date of KiCad file format required to open file.
int lineNumber
at which line number, 1 based index.
wxString parseProblem
std::string inputLine
problem line of input [say, from a LINE_READER].
PARSE_ERROR(const wxString &aProblem, const char *aThrowersFile, const char *aThrowersFunction, int aThrowersLineNumber, const wxString &aSource, const char *aInputLine, int aLineNumber, int aByteIndex)
Normally called via the macro THROW_PARSE_ERROR so that FILE, FUNCTION, and LINE can be captured from...
int byteIndex
at which byte offset within the line, 1 based index
void init(const wxString &aProblem, const char *aThrowersFile, const char *aThrowersFunction, int aThrowersLineNumber, const wxString &aSource, const char *aInputLine, int aLineNumber, int aByteIndex)