@ -40,7 +40,11 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling
public RemoteServiceErrorInfo Convert ( Exception exception , bool includeSensitiveDetails )
public RemoteServiceErrorInfo Convert ( Exception exception , bool includeSensitiveDetails )
{
{
var errorInfo = CreateErrorInfoWithoutCode ( exception , includeSensitiveDetails ) ;
var exceptionHandlingOptions = CreateDefaultOptions ( ) ;
exceptionHandlingOptions . SendExceptionsDetailsToClients = includeSensitiveDetails ;
exceptionHandlingOptions . SendStackTraceToClients = includeSensitiveDetails ;
var errorInfo = CreateErrorInfoWithoutCode ( exception , exceptionHandlingOptions ) ;
if ( exception is IHasErrorCode hasErrorCodeException )
if ( exception is IHasErrorCode hasErrorCodeException )
{
{
@ -50,11 +54,26 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling
return errorInfo ;
return errorInfo ;
}
}
protected virtual RemoteServiceErrorInfo CreateErrorInfoWithoutCode ( Exception exception , bool includeSensitiveDetails )
public RemoteServiceErrorInfo Convert ( Exception exception , Action < AbpExceptionHandlingOptions > options = null )
{
{
if ( includeSensitiveDetails )
var exceptionHandlingOptions = CreateDefaultOptions ( ) ;
options ? . Invoke ( exceptionHandlingOptions ) ;
var errorInfo = CreateErrorInfoWithoutCode ( exception , exceptionHandlingOptions ) ;
if ( exception is IHasErrorCode hasErrorCodeException )
{
errorInfo . Code = hasErrorCodeException . Code ;
}
return errorInfo ;
}
protected virtual RemoteServiceErrorInfo CreateErrorInfoWithoutCode ( Exception exception , AbpExceptionHandlingOptions options )
{
if ( options . SendExceptionsDetailsToClients )
{
{
return CreateDetailedErrorInfoFromException ( exception ) ;
return CreateDetailedErrorInfoFromException ( exception , options . SendStackTraceToClients ) ;
}
}
exception = TryToGetActualException ( exception ) ;
exception = TryToGetActualException ( exception ) ;
@ -194,11 +213,11 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling
return exception ;
return exception ;
}
}
protected virtual RemoteServiceErrorInfo CreateDetailedErrorInfoFromException ( Exception exception )
protected virtual RemoteServiceErrorInfo CreateDetailedErrorInfoFromException ( Exception exception , bool sendStackTraceToClients )
{
{
var detailBuilder = new StringBuilder ( ) ;
var detailBuilder = new StringBuilder ( ) ;
AddExceptionToDetails ( exception , detailBuilder ) ;
AddExceptionToDetails ( exception , detailBuilder , sendStackTraceToClients ) ;
var errorInfo = new RemoteServiceErrorInfo ( exception . Message , detailBuilder . ToString ( ) ) ;
var errorInfo = new RemoteServiceErrorInfo ( exception . Message , detailBuilder . ToString ( ) ) ;
@ -210,7 +229,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling
return errorInfo ;
return errorInfo ;
}
}
protected virtual void AddExceptionToDetails ( Exception exception , StringBuilder detailBuilder )
protected virtual void AddExceptionToDetails ( Exception exception , StringBuilder detailBuilder , bool sendStackTraceToClients )
{
{
//Exception Message
//Exception Message
detailBuilder . AppendLine ( exception . GetType ( ) . Name + ": " + exception . Message ) ;
detailBuilder . AppendLine ( exception . GetType ( ) . Name + ": " + exception . Message ) ;
@ -237,7 +256,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling
}
}
//Exception StackTrace
//Exception StackTrace
if ( ! string . IsNullOrEmpty ( exception . StackTrace ) )
if ( sendStackTraceToClients & & ! string . IsNullOrEmpty ( exception . StackTrace ) )
{
{
detailBuilder . AppendLine ( "STACK TRACE: " + exception . StackTrace ) ;
detailBuilder . AppendLine ( "STACK TRACE: " + exception . StackTrace ) ;
}
}
@ -245,7 +264,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling
//Inner exception
//Inner exception
if ( exception . InnerException ! = null )
if ( exception . InnerException ! = null )
{
{
AddExceptionToDetails ( exception . InnerException , detailBuilder ) ;
AddExceptionToDetails ( exception . InnerException , detailBuilder , sendStackTraceToClients ) ;
}
}
//Inner exceptions for AggregateException
//Inner exceptions for AggregateException
@ -259,7 +278,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling
foreach ( var innerException in aggException . InnerExceptions )
foreach ( var innerException in aggException . InnerExceptions )
{
{
AddExceptionToDetails ( innerException , detailBuilder ) ;
AddExceptionToDetails ( innerException , detailBuilder , sendStackTraceToClients ) ;
}
}
}
}
}
}
@ -296,5 +315,14 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling
return detailBuilder . ToString ( ) ;
return detailBuilder . ToString ( ) ;
}
}
protected virtual AbpExceptionHandlingOptions CreateDefaultOptions ( )
{
return new AbpExceptionHandlingOptions
{
SendExceptionsDetailsToClients = false ,
SendStackTraceToClients = true
} ;
}
}
}
}
}