@@ -14,6 +14,7 @@ This Source Code Form is subject to the terms of the
1414using System ;
1515using System . Collections . Generic ;
1616using System . Threading . Tasks ;
17+ using Microsoft . AspNetCore . Diagnostics ;
1718using ExecutionContext = ScriptEngine . Machine . ExecutionContext ;
1819
1920namespace OneScript . Web . Server
@@ -91,40 +92,14 @@ private void ConfigureApp()
9192 _app . UseStaticFiles ( ) ;
9293
9394 if ( _exceptionHandler != null )
94- _app . UseExceptionHandler ( handler =>
95- {
96- handler . Run ( context =>
97- {
98- var args = new IValue [ ]
99- {
100- new HttpContextWrapper ( _executionContext . TypeManager , context ) ,
101- } ;
102-
103- var methodNumber = _exceptionHandler ? . Target . GetMethodNumber ( _exceptionHandler ? . MethodName ) ;
104-
105- var debugController = _executionContext . Services . TryResolve < IDebugController > ( ) ;
106-
107- // Thread unsafe call!
108- debugController ? . AttachToThread ( ) ;
109-
110- try
111- {
112- _exceptionHandler ? . Target . CallAsProcedure ( ( int ) methodNumber , args ) ;
113- }
114- catch ( Exception ex )
115- {
116- WriteExceptionToResponse ( context , ex ) ;
117- }
118- finally
119- {
120- // Thread unsafe call!
121- debugController ? . DetachFromThread ( ) ;
122- }
123-
124- return Task . CompletedTask ;
125- } ) ;
126- } ) ;
127-
95+ {
96+ UseBslExceptionHandler ( ) ;
97+ }
98+ else
99+ {
100+ UseDefaultExceptionHandler ( ) ;
101+ }
102+
128103 if ( _useWebSockets )
129104 _app . UseWebSockets ( ) ;
130105
@@ -147,15 +122,64 @@ private void ConfigureApp()
147122 {
148123 middleware . Target . CallAsProcedure ( methodNumber , args ) ;
149124 }
125+ finally
126+ {
127+ debugController ? . DetachFromThread ( ) ;
128+ }
129+
130+ return Task . CompletedTask ;
131+ } ) ;
132+ } ) ;
133+ }
134+
135+ private void UseDefaultExceptionHandler ( )
136+ {
137+ _app . UseExceptionHandler ( errApp =>
138+ {
139+ errApp . Run ( context =>
140+ {
141+ var exceptionHandlerPathFeature =
142+ context . Features . Get < IExceptionHandlerPathFeature > ( ) ;
143+
144+ WriteExceptionToResponse ( context , exceptionHandlerPathFeature ? . Error ) ;
145+
146+ return Task . CompletedTask ;
147+ } ) ;
148+ } ) ;
149+ }
150+
151+ private void UseBslExceptionHandler ( )
152+ {
153+ _app . UseExceptionHandler ( handler =>
154+ {
155+ handler . Run ( context =>
156+ {
157+ var args = new IValue [ ]
158+ {
159+ new HttpContextWrapper ( _executionContext . TypeManager , context ) ,
160+ } ;
161+
162+ var methodNumber = _exceptionHandler ? . Target . GetMethodNumber ( _exceptionHandler ? . MethodName ) ;
163+
164+ var debugController = _executionContext . Services . TryResolve < IDebugController > ( ) ;
165+
166+ // Thread unsafe call!
167+ debugController ? . AttachToThread ( ) ;
168+
169+ try
170+ {
171+ _exceptionHandler ? . Target . CallAsProcedure ( ( int ) methodNumber , args ) ;
172+ }
150173 catch ( Exception ex )
151174 {
152- if ( _exceptionHandler == null )
175+ if ( ! context . Response . HasStarted )
153176 WriteExceptionToResponse ( context , ex ) ;
154177 else
155178 throw ;
156179 }
157180 finally
158181 {
182+ // Thread unsafe call!
159183 debugController ? . DetachFromThread ( ) ;
160184 }
161185
0 commit comments