Skip to content

Commit 70deae2

Browse files
brettfobaronfel
authored andcommitted
create a specific exception type to represent FSI compilation errors (#7713)
1 parent ff1d3f3 commit 70deae2

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/fsharp/fsi/FSIstrings.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,5 @@ fsiBindingSessionTo,"Binding session to '%s'..."
5252
fsiProductName,"Microsoft (R) F# Interactive version %s"
5353
fsiProductNameCommunity,"F# Interactive for F# %s"
5454
shadowCopyReferences,"Prevents references from being locked by the F# Interactive process"
55+
fsiOperationCouldNotBeCompleted,"Operation could not be completed due to earlier error"
56+
fsiOperationFailed,"Operation failed. The error text has been printed in the error stream. To return the corresponding FSharpErrorInfo use the EvalInteractionNonThrowing, EvalScriptNonThrowing or EvalExpressionNonThrowing"

src/fsharp/fsi/fsi.fs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,6 +2280,11 @@ let internal DriveFsiEventLoop (fsi: FsiEvaluationSessionHostConfig, fsiConsoleO
22802280

22812281
runLoop();
22822282

2283+
/// Thrown when there was an error compiling the given code in FSI.
2284+
type FsiCompilationException(message: string, errorInfos: FSharpErrorInfo[] option) =
2285+
inherit System.Exception(message)
2286+
member __.ErrorInfos = errorInfos
2287+
22832288
/// The primary type, representing a full F# Interactive session, reading from the given
22842289
/// text input, writing to the given text output and error writers.
22852290
type FsiEvaluationSession (fsi: FsiEvaluationSessionHostConfig, argv:string[], inReader:TextReader, outWriter:TextWriter, errorWriter: TextWriter, fsiCollectible: bool, legacyReferenceResolver: ReferenceResolver.Resolver option) =
@@ -2443,21 +2448,22 @@ type FsiEvaluationSession (fsi: FsiEvaluationSessionHostConfig, argv:string[], i
24432448
let fsiInteractionProcessor = FsiInteractionProcessor(fsi, tcConfigB, fsiOptions, fsiDynamicCompiler, fsiConsolePrompt, fsiConsoleOutput, fsiInterruptController, fsiStdinLexerProvider, lexResourceManager, initialInteractiveState)
24442449

24452450
let commitResult res =
2446-
match res with
2451+
match res with
24472452
| Choice1Of2 r -> r
2448-
| Choice2Of2 None -> failwith "Operation failed. The error text has been printed in the error stream. To return the corresponding FSharpErrorInfo use the EvalInteractionNonThrowing, EvalScriptNonThrowing or EvalExpressionNonThrowing"
2453+
| Choice2Of2 None -> raise (FsiCompilationException(FSIstrings.SR.fsiOperationFailed(), None))
24492454
| Choice2Of2 (Some userExn) -> raise userExn
24502455

2451-
let commitResultNonThrowing errorOptions scriptFile (errorLogger: CompilationErrorLogger) res =
2456+
let commitResultNonThrowing errorOptions scriptFile (errorLogger: CompilationErrorLogger) res =
24522457
let errs = errorLogger.GetErrors()
2453-
let userRes =
2454-
match res with
2458+
let errorInfos = ErrorHelpers.CreateErrorInfos (errorOptions, true, scriptFile, errs, true)
2459+
let userRes =
2460+
match res with
24552461
| Choice1Of2 r -> Choice1Of2 r
2456-
| Choice2Of2 None -> Choice2Of2 (System.Exception "Operation could not be completed due to earlier error")
2462+
| Choice2Of2 None -> Choice2Of2 (FsiCompilationException(FSIstrings.SR.fsiOperationCouldNotBeCompleted(), Some errorInfos) :> exn)
24572463
| Choice2Of2 (Some userExn) -> Choice2Of2 userExn
24582464

24592465
// 'true' is passed for "suggestNames" because we want the FSI session to suggest names for misspellings and it won't affect IDE perf much
2460-
userRes, ErrorHelpers.CreateErrorInfos (errorOptions, true, scriptFile, errs, true)
2466+
userRes, errorInfos
24612467

24622468
let dummyScriptFileName = "input.fsx"
24632469

src/fsharp/fsi/fsi.fsi

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ type public FsiEvaluationSessionHostConfig =
106106
/// Implicitly reference FSharp.Compiler.Interactive.Settings.dll
107107
abstract UseFsiAuxLib : bool
108108

109+
/// Thrown when there was an error compiling the given code in FSI.
110+
[<Class>]
111+
type FsiCompilationException =
112+
inherit System.Exception
113+
new : string * FSharpErrorInfo[] option -> FsiCompilationException
114+
member ErrorInfos : FSharpErrorInfo[] option
109115

110116
/// Represents an F# Interactive evaluation session.
111117
[<Class>]
@@ -184,7 +190,7 @@ type FsiEvaluationSession =
184190
///
185191
/// Due to a current limitation, it is not fully thread-safe to run this operation concurrently with evaluation triggered
186192
/// by input from 'stdin'.
187-
member EvalExpressionNonThrowing : code: string -> Choice<FsiValue option, exn> * FSharpErrorInfo[]
193+
member EvalExpressionNonThrowing : code: string -> Choice<FsiValue option, exn> * FSharpErrorInfo[]
188194

189195
/// Format a value to a string using the current PrintDepth, PrintLength etc settings provided by the active fsi configuration object
190196
member FormatValue : reflectionValue: obj * reflectionType: System.Type -> string

0 commit comments

Comments
 (0)