1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
49 #include <comphelper/anytostring.hxx>
50 #include <sal/log.hxx>
51 #include <osl/thread.h>
52 #include <rtl/strbuf.hxx>
54 #include <comphelper/diagnose_ex.hxx>
56 #if defined __GLIBCXX__
60 static void exceptionToStringImpl(OStringBuffer
& sMessage
, const css::uno::Any
& caught
)
62 auto toOString
= [](OUString
const & s
) {
63 return OUStringToOString( s
, osl_getThreadTextEncoding() );
65 // when called recursively, we might not have any exception to print
66 if (!caught
.hasValue())
68 sMessage
.append(toOString(caught
.getValueTypeName()));
69 css::uno::Exception exception
;
71 if ( !exception
.Message
.isEmpty() )
73 sMessage
.append(" message: \"");
74 sMessage
.append(toOString(exception
.Message
));
75 sMessage
.append("\"");
77 if ( exception
.Context
.is() )
79 const char* pContext
= typeid( *exception
.Context
).name();
80 #if defined __GLIBCXX__
81 // demangle the type name, not necessary under windows, we already get demangled names there
83 pContext
= abi::__cxa_demangle( pContext
, nullptr, nullptr, &status
);
85 sMessage
.append(" context: ");
86 sMessage
.append(pContext
);
87 #if defined __GLIBCXX__
88 std::free(const_cast<char *>(pContext
));
92 css::configuration::CorruptedConfigurationException specialized
;
93 if ( caught
>>= specialized
)
95 sMessage
.append(" details: ");
96 sMessage
.append(toOString(specialized
.Details
));
100 css::configuration::InvalidBootstrapFileException specialized
;
101 if ( caught
>>= specialized
)
103 sMessage
.append(" BootstrapFileURL: ");
104 sMessage
.append(toOString(specialized
.BootstrapFileURL
));
108 css::configuration::MissingBootstrapFileException specialized
;
109 if ( caught
>>= specialized
)
111 sMessage
.append(" BootstrapFileURL: ");
112 sMessage
.append(toOString(specialized
.BootstrapFileURL
));
116 css::configuration::backend::MalformedDataException specialized
;
117 if ( caught
>>= specialized
)
119 sMessage
.append("\n wrapped: ");
120 exceptionToStringImpl(sMessage
, specialized
.ErrorDetails
);
124 css::configuration::backend::BackendSetupException specialized
;
125 if ( caught
>>= specialized
)
127 sMessage
.append("\n wrapped: ");
128 exceptionToStringImpl(sMessage
, specialized
.BackendException
);
132 css::deployment::DependencyException specialized
;
133 if ( caught
>>= specialized
)
135 sMessage
.append(" UnsatisfiedDependencies: ");
136 sMessage
.append(toOString(comphelper::anyToString(css::uno::Any(specialized
.UnsatisfiedDependencies
))));
140 css::deployment::DeploymentException specialized
;
141 if ( caught
>>= specialized
)
143 sMessage
.append("\n wrapped: ");
144 exceptionToStringImpl(sMessage
, specialized
.Cause
);
148 css::document::CorruptedFilterConfigurationException specialized
;
149 if ( caught
>>= specialized
)
151 sMessage
.append(" Details: ");
152 sMessage
.append(toOString(specialized
.Details
));
156 css::document::UndoFailedException specialized
;
157 if ( caught
>>= specialized
)
159 sMessage
.append(" Reason: ");
160 sMessage
.append(toOString(comphelper::anyToString(specialized
.Reason
)));
164 css::lang::IllegalArgumentException specialized
;
165 if ( caught
>>= specialized
)
167 sMessage
.append(" ArgumentPosition: ");
168 sMessage
.append(static_cast<sal_Int32
>(specialized
.ArgumentPosition
));
172 css::lang::WrappedTargetException specialized
;
173 if ( caught
>>= specialized
)
175 sMessage
.append("\n wrapped: ");
176 exceptionToStringImpl(sMessage
, specialized
.TargetException
);
180 css::lang::WrappedTargetRuntimeException specialized
;
181 if ( caught
>>= specialized
)
183 sMessage
.append("\n wrapped: ");
184 exceptionToStringImpl(sMessage
, specialized
.TargetException
);
188 css::ldap::LdapGenericException specialized
;
189 if ( caught
>>= specialized
)
191 sMessage
.append(" ErrorCode: ");
192 sMessage
.append(specialized
.ErrorCode
);
196 css::script::BasicErrorException specialized
;
197 if ( caught
>>= specialized
)
199 sMessage
.append(" ErrorCode: ");
200 sMessage
.append(specialized
.ErrorCode
);
201 sMessage
.append(" ErrorMessageArgument: ");
202 sMessage
.append(toOString(specialized
.ErrorMessageArgument
));
206 css::script::CannotConvertException specialized
;
207 if ( caught
>>= specialized
)
209 sMessage
.append(" DestinationTypeClass: ");
210 sMessage
.append(toOString(comphelper::anyToString(css::uno::Any(specialized
.DestinationTypeClass
))));
211 sMessage
.append(" Reason: ");
212 sMessage
.append(specialized
.Reason
);
213 sMessage
.append(" ArgumentIndex: ");
214 sMessage
.append(specialized
.ArgumentIndex
);
218 css::script::provider::ScriptErrorRaisedException specialized
;
219 if ( caught
>>= specialized
)
221 sMessage
.append(" scriptName: ");
222 sMessage
.append(toOString(specialized
.scriptName
));
223 sMessage
.append(" language: ");
224 sMessage
.append(toOString(specialized
.language
));
225 sMessage
.append(" lineNum: ");
226 sMessage
.append(specialized
.lineNum
);
230 css::script::provider::ScriptExceptionRaisedException specialized
;
231 if ( caught
>>= specialized
)
233 sMessage
.append(" exceptionType: ");
234 sMessage
.append(toOString(specialized
.exceptionType
));
238 css::script::provider::ScriptFrameworkErrorException specialized
;
239 if ( caught
>>= specialized
)
241 sMessage
.append(" scriptName: ");
242 sMessage
.append(toOString(specialized
.scriptName
));
243 sMessage
.append(" language: ");
244 sMessage
.append(toOString(specialized
.language
));
245 sMessage
.append(" errorType: ");
246 sMessage
.append(specialized
.errorType
);
250 css::sdbc::SQLException specialized
;
251 if ( caught
>>= specialized
)
253 sMessage
.append(" SQLState: ");
254 sMessage
.append(toOString(specialized
.SQLState
));
255 sMessage
.append(" ErrorCode: ");
256 sMessage
.append(specialized
.ErrorCode
);
257 sMessage
.append("\n wrapped: ");
258 exceptionToStringImpl(sMessage
, specialized
.NextException
);
262 css::system::SystemShellExecuteException specialized
;
263 if ( caught
>>= specialized
)
265 sMessage
.append(" PosixError: ");
266 sMessage
.append(specialized
.PosixError
);
270 css::task::ErrorCodeIOException specialized
;
271 if ( caught
>>= specialized
)
273 sMessage
.append(" errcode: ");
274 sMessage
.append( specialized
.ErrCode
);
278 css::ucb::CommandFailedException specialized
;
279 if ( caught
>>= specialized
)
281 sMessage
.append("\n Reason: ");
282 exceptionToStringImpl( sMessage
, specialized
.Reason
);
286 css::ucb::ContentCreationException specialized
;
287 if ( caught
>>= specialized
)
289 sMessage
.append(" eError: ");
290 sMessage
.append(toOString(comphelper::anyToString( css::uno::Any(specialized
.eError
) )));
294 css::ucb::MissingPropertiesException specialized
;
295 if ( caught
>>= specialized
)
297 sMessage
.append(" Properties: ");
298 sMessage
.append(toOString(comphelper::anyToString( css::uno::Any(specialized
.Properties
) )));
302 css::ucb::NameClashException specialized
;
303 if ( caught
>>= specialized
)
305 sMessage
.append(" Name: ");
306 sMessage
.append(toOString( specialized
.Name
));
310 css::util::MalformedNumberFormatException specialized
;
311 if ( caught
>>= specialized
)
313 sMessage
.append(" CheckPos: ");
314 sMessage
.append( specialized
.CheckPos
);
318 css::xml::dom::DOMException specialized
;
319 if ( caught
>>= specialized
)
321 sMessage
.append(" Code: ");
322 sMessage
.append(toOString(comphelper::anyToString( css::uno::Any(specialized
.Code
) )));
326 css::xml::sax::SAXException specialized
;
327 if ( caught
>>= specialized
)
329 sMessage
.append("\n wrapped: ");
330 exceptionToStringImpl( sMessage
, specialized
.WrappedException
);
334 css::xml::sax::SAXParseException specialized
;
335 if ( caught
>>= specialized
)
337 sMessage
.append(" PublicId: ");
338 sMessage
.append(toOString( specialized
.PublicId
));
339 sMessage
.append(" SystemId: ");
340 sMessage
.append(toOString( specialized
.SystemId
));
341 sMessage
.append(" LineNumber: ");
342 sMessage
.append( specialized
.LineNumber
);
343 sMessage
.append(" ColumnNumber: ");
344 sMessage
.append( specialized
.ColumnNumber
);
348 css::ucb::InteractiveIOException specialized
;
349 if ( caught
>>= specialized
)
351 sMessage
.append(" Code: ");
352 sMessage
.append( static_cast<sal_Int32
>(specialized
.Code
) );
357 OString
exceptionToString(const css::uno::Any
& caught
)
359 OStringBuffer
sMessage(512);
360 exceptionToStringImpl(sMessage
, caught
);
361 return sMessage
.makeStringAndClear();
364 void DbgUnhandledException(const css::uno::Any
& caught
, const char* currentFunction
, const char* fileAndLineNo
,
365 const char* area
, const char* explanatory
)
367 OStringBuffer
sMessage( 512 );
368 sMessage
.append( OString::Concat("DBG_UNHANDLED_EXCEPTION in ") + currentFunction
);
371 sMessage
.append(OString::Concat("\n when: ") + explanatory
);
373 sMessage
.append(" exception: ");
374 exceptionToStringImpl(sMessage
, caught
);
379 SAL_DETAIL_LOG_FORMAT(
380 SAL_DETAIL_ENABLE_LOG_WARN
, SAL_DETAIL_LOG_LEVEL_WARN
,
381 area
, fileAndLineNo
, "%s", sMessage
.getStr());
384 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */