LibreOffice Module comphelper (master) 1
diagnose_ex.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <com/sun/star/configuration/CorruptedConfigurationException.hpp>
21#include <com/sun/star/configuration/backend/BackendSetupException.hpp>
22#include <com/sun/star/configuration/backend/MalformedDataException.hpp>
23#include <com/sun/star/configuration/InvalidBootstrapFileException.hpp>
24#include <com/sun/star/configuration/MissingBootstrapFileException.hpp>
25#include <com/sun/star/deployment/DependencyException.hpp>
26#include <com/sun/star/deployment/DeploymentException.hpp>
27#include <com/sun/star/document/CorruptedFilterConfigurationException.hpp>
28#include <com/sun/star/document/UndoFailedException.hpp>
29#include <com/sun/star/lang/IllegalArgumentException.hpp>
30#include <com/sun/star/lang/WrappedTargetException.hpp>
31#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
32#include <com/sun/star/ldap/LdapGenericException.hpp>
33#include <com/sun/star/script/BasicErrorException.hpp>
34#include <com/sun/star/script/CannotConvertException.hpp>
35#include <com/sun/star/script/provider/ScriptExceptionRaisedException.hpp>
36#include <com/sun/star/script/provider/ScriptFrameworkErrorException.hpp>
37#include <com/sun/star/sdbc/SQLException.hpp>
38#include <com/sun/star/system/SystemShellExecuteException.hpp>
39#include <com/sun/star/task/ErrorCodeIOException.hpp>
40#include <com/sun/star/ucb/CommandFailedException.hpp>
41#include <com/sun/star/ucb/ContentCreationException.hpp>
42#include <com/sun/star/ucb/MissingPropertiesException.hpp>
43#include <com/sun/star/ucb/NameClashException.hpp>
44#include <com/sun/star/ucb/InteractiveIOException.hpp>
45#include <com/sun/star/util/MalformedNumberFormatException.hpp>
46#include <com/sun/star/xml/dom/DOMException.hpp>
47#include <com/sun/star/xml/sax/SAXException.hpp>
48#include <com/sun/star/xml/sax/SAXParseException.hpp>
50#include <sal/log.hxx>
51#include <osl/thread.h>
52#include <rtl/strbuf.hxx>
53
55
56#if defined __GLIBCXX__
57#include <cxxabi.h>
58#endif
59
60static void exceptionToStringImpl(OStringBuffer& sMessage, const css::uno::Any & caught)
61{
62 auto toOString = [](OUString const & s) {
63 return OUStringToOString( s, osl_getThreadTextEncoding() );
64 };
65 sMessage.append(toOString(caught.getValueTypeName()));
66 css::uno::Exception exception;
67 caught >>= exception;
68 if ( !exception.Message.isEmpty() )
69 {
70 sMessage.append(" message: \"");
71 sMessage.append(toOString(exception.Message));
72 sMessage.append("\"");
73 }
74 if ( exception.Context.is() )
75 {
76 const char* pContext = typeid( *exception.Context ).name();
77#if defined __GLIBCXX__
78 // demangle the type name, not necessary under windows, we already get demangled names there
79 int status;
80 pContext = abi::__cxa_demangle( pContext, nullptr, nullptr, &status);
81#endif
82 sMessage.append(" context: ");
83 sMessage.append(pContext);
84#if defined __GLIBCXX__
85 std::free(const_cast<char *>(pContext));
86#endif
87 }
88 {
89 css::configuration::CorruptedConfigurationException specialized;
90 if ( caught >>= specialized )
91 {
92 sMessage.append(" details: ");
93 sMessage.append(toOString(specialized.Details));
94 }
95 }
96 {
97 css::configuration::InvalidBootstrapFileException specialized;
98 if ( caught >>= specialized )
99 {
100 sMessage.append(" BootstrapFileURL: ");
101 sMessage.append(toOString(specialized.BootstrapFileURL));
102 }
103 }
104 {
105 css::configuration::MissingBootstrapFileException specialized;
106 if ( caught >>= specialized )
107 {
108 sMessage.append(" BootstrapFileURL: ");
109 sMessage.append(toOString(specialized.BootstrapFileURL));
110 }
111 }
112 {
113 css::configuration::backend::MalformedDataException specialized;
114 if ( caught >>= specialized )
115 {
116 sMessage.append("\n wrapped: ");
117 sMessage.append(exceptionToString(specialized.ErrorDetails));
118 }
119 }
120 {
121 css::configuration::backend::BackendSetupException specialized;
122 if ( caught >>= specialized )
123 {
124 sMessage.append("\n wrapped: ");
125 sMessage.append(exceptionToString(specialized.BackendException));
126 }
127 }
128 {
129 css::deployment::DependencyException specialized;
130 if ( caught >>= specialized )
131 {
132 sMessage.append(" UnsatisfiedDependencies: ");
133 sMessage.append(toOString(comphelper::anyToString(css::uno::Any(specialized.UnsatisfiedDependencies))));
134 }
135 }
136 {
137 css::deployment::DeploymentException specialized;
138 if ( caught >>= specialized )
139 {
140 sMessage.append("\n wrapped: ");
141 sMessage.append(exceptionToString(specialized.Cause));
142 }
143 }
144 {
145 css::document::CorruptedFilterConfigurationException specialized;
146 if ( caught >>= specialized )
147 {
148 sMessage.append(" Details: ");
149 sMessage.append(toOString(specialized.Details));
150 }
151 }
152 {
153 css::document::UndoFailedException specialized;
154 if ( caught >>= specialized )
155 {
156 sMessage.append(" Reason: ");
157 sMessage.append(toOString(comphelper::anyToString(specialized.Reason)));
158 }
159 }
160 {
161 css::lang::IllegalArgumentException specialized;
162 if ( caught >>= specialized )
163 {
164 sMessage.append(" ArgumentPosition: ");
165 sMessage.append(static_cast<sal_Int32>(specialized.ArgumentPosition));
166 }
167 }
168 {
169 css::lang::WrappedTargetException specialized;
170 if ( caught >>= specialized )
171 {
172 sMessage.append("\n wrapped: ");
173 sMessage.append(exceptionToString(specialized.TargetException));
174 }
175 }
176 {
177 css::lang::WrappedTargetRuntimeException specialized;
178 if ( caught >>= specialized )
179 {
180 sMessage.append("\n wrapped: ");
181 sMessage.append(exceptionToString(specialized.TargetException));
182 }
183 }
184 {
185 css::ldap::LdapGenericException specialized;
186 if ( caught >>= specialized )
187 {
188 sMessage.append(" ErrorCode: ");
189 sMessage.append(specialized.ErrorCode);
190 }
191 }
192 {
193 css::script::BasicErrorException specialized;
194 if ( caught >>= specialized )
195 {
196 sMessage.append(" ErrorCode: ");
197 sMessage.append(specialized.ErrorCode);
198 sMessage.append(" ErrorMessageArgument: ");
199 sMessage.append(toOString(specialized.ErrorMessageArgument));
200 }
201 }
202 {
203 css::script::CannotConvertException specialized;
204 if ( caught >>= specialized )
205 {
206 sMessage.append(" DestinationTypeClass: ");
207 sMessage.append(toOString(comphelper::anyToString(css::uno::Any(specialized.DestinationTypeClass))));
208 sMessage.append(" Reason: ");
209 sMessage.append(specialized.Reason);
210 sMessage.append(" ArgumentIndex: ");
211 sMessage.append(specialized.ArgumentIndex);
212 }
213 }
214 {
215 css::script::provider::ScriptErrorRaisedException specialized;
216 if ( caught >>= specialized )
217 {
218 sMessage.append(" scriptName: ");
219 sMessage.append(toOString(specialized.scriptName));
220 sMessage.append(" language: ");
221 sMessage.append(toOString(specialized.language));
222 sMessage.append(" lineNum: ");
223 sMessage.append(specialized.lineNum);
224 }
225 }
226 {
227 css::script::provider::ScriptExceptionRaisedException specialized;
228 if ( caught >>= specialized )
229 {
230 sMessage.append(" exceptionType: ");
231 sMessage.append(toOString(specialized.exceptionType));
232 }
233 }
234 {
235 css::script::provider::ScriptFrameworkErrorException specialized;
236 if ( caught >>= specialized )
237 {
238 sMessage.append(" scriptName: ");
239 sMessage.append(toOString(specialized.scriptName));
240 sMessage.append(" language: ");
241 sMessage.append(toOString(specialized.language));
242 sMessage.append(" errorType: ");
243 sMessage.append(specialized.errorType);
244 }
245 }
246 {
247 css::sdbc::SQLException specialized;
248 if ( caught >>= specialized )
249 {
250 sMessage.append(" SQLState: ");
251 sMessage.append(toOString(specialized.SQLState));
252 sMessage.append(" ErrorCode: ");
253 sMessage.append(specialized.ErrorCode);
254 sMessage.append("\n wrapped: ");
255 sMessage.append(exceptionToString(specialized.NextException));
256 }
257 }
258 {
259 css::system::SystemShellExecuteException specialized;
260 if ( caught >>= specialized )
261 {
262 sMessage.append(" PosixError: ");
263 sMessage.append(specialized.PosixError);
264 }
265 }
266 {
267 css::task::ErrorCodeIOException specialized;
268 if ( caught >>= specialized )
269 {
270 sMessage.append(" errcode: ");
271 sMessage.append( specialized.ErrCode );
272 }
273 }
274 {
275 css::ucb::CommandFailedException specialized;
276 if ( caught >>= specialized )
277 {
278 sMessage.append("\n Reason: ");
279 sMessage.append(exceptionToString( specialized.Reason ));
280 }
281 }
282 {
283 css::ucb::ContentCreationException specialized;
284 if ( caught >>= specialized )
285 {
286 sMessage.append(" eError: ");
287 sMessage.append(toOString(comphelper::anyToString( css::uno::Any(specialized.eError) )));
288 }
289 }
290 {
291 css::ucb::MissingPropertiesException specialized;
292 if ( caught >>= specialized )
293 {
294 sMessage.append(" Properties: ");
295 sMessage.append(toOString(comphelper::anyToString( css::uno::Any(specialized.Properties) )));
296 }
297 }
298 {
299 css::ucb::NameClashException specialized;
300 if ( caught >>= specialized )
301 {
302 sMessage.append(" Name: ");
303 sMessage.append(toOString( specialized.Name ));
304 }
305 }
306 {
307 css::util::MalformedNumberFormatException specialized;
308 if ( caught >>= specialized )
309 {
310 sMessage.append(" CheckPos: ");
311 sMessage.append( specialized.CheckPos );
312 }
313 }
314 {
315 css::xml::dom::DOMException specialized;
316 if ( caught >>= specialized )
317 {
318 sMessage.append(" Code: ");
319 sMessage.append(toOString(comphelper::anyToString( css::uno::Any(specialized.Code) )));
320 }
321 }
322 {
323 css::xml::dom::DOMException specialized;
324 if ( caught >>= specialized )
325 {
326 sMessage.append(" Code: ");
327 sMessage.append(toOString(comphelper::anyToString( css::uno::Any(specialized.Code) )));
328 }
329 }
330 {
331 css::xml::sax::SAXException specialized;
332 if ( caught >>= specialized )
333 {
334 sMessage.append("\n wrapped: ");
335 sMessage.append(exceptionToString( specialized.WrappedException ));
336 }
337 }
338 {
339 css::xml::sax::SAXParseException specialized;
340 if ( caught >>= specialized )
341 {
342 sMessage.append(" PublicId: ");
343 sMessage.append(toOString( specialized.PublicId ));
344 sMessage.append(" SystemId: ");
345 sMessage.append(toOString( specialized.SystemId ));
346 sMessage.append(" LineNumber: ");
347 sMessage.append( specialized.LineNumber );
348 sMessage.append(" ColumnNumber: ");
349 sMessage.append( specialized.ColumnNumber );
350 }
351 }
352 {
353 css::ucb::InteractiveIOException specialized;
354 if ( caught >>= specialized )
355 {
356 sMessage.append(" Code: ");
357 sMessage.append( static_cast<sal_Int32>(specialized.Code) );
358 }
359 }
360}
361
362OString exceptionToString(const css::uno::Any & caught)
363{
364 OStringBuffer sMessage(512);
366 return sMessage.makeStringAndClear();
367}
368
369void DbgUnhandledException(const css::uno::Any & caught, const char* currentFunction, const char* fileAndLineNo,
370 const char* area, const char* explanatory)
371{
372 OStringBuffer sMessage( 512 );
373 sMessage.append( OString::Concat("DBG_UNHANDLED_EXCEPTION in ") + currentFunction);
374 if (explanatory)
375 {
376 sMessage.append(OString::Concat("\n when: ") + explanatory);
377 }
378 sMessage.append(" exception: ");
380
381 if (area == nullptr)
382 area = "legacy.osl";
383
384 SAL_DETAIL_LOG_FORMAT(
385 SAL_DETAIL_ENABLE_LOG_WARN, SAL_DETAIL_LOG_LEVEL_WARN,
386 area, fileAndLineNo, "%s", sMessage.getStr());
387}
388
389/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
void DbgUnhandledException(const css::uno::Any &caught, const char *currentFunction, const char *fileAndLineNo, const char *area, const char *explanatory)
OString exceptionToString(const css::uno::Any &caught)
Convert a caught exception to a string suitable for logging.
static void exceptionToStringImpl(OStringBuffer &sMessage, const css::uno::Any &caught)
Definition: diagnose_ex.cxx:60
const char * name
OUString anyToString(uno::Any const &value)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
OUString sMessage