Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Fable.Transforms/Beam/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,16 @@ let private operators
| "FailWith", [ msg ]
| "InvalidOp", [ msg ] -> makeThrow r _t msg |> Some
| "InvalidArg", [ argName; msg ] ->
let msg = add (add msg (Value(StringConstant "\\nParameter name: ", None))) argName
let msg =
add msg (add (add (Value(StringConstant " (Parameter '", None)) argName) (Value(StringConstant "')", None)))

makeThrow r _t msg |> Some
| "Raise", [ arg ] -> makeThrow r _t arg |> Some
| "NullArg", [ arg ] ->
let msg =
add
(Value(StringConstant "Value cannot be null.", None))
(add (Value(StringConstant "\\nParameter name: ", None)) arg)
(Value(StringConstant "Value cannot be null. (Parameter '", None))
(add arg (Value(StringConstant "')", None)))

makeThrow r _t msg |> Some
| "IsNull", [ arg ] -> emitExpr r _t [ arg ] "($0 =:= undefined)" |> Some
Expand All @@ -150,7 +152,7 @@ let private operators
r
_t
[ argName; arg ]
"case $1 of undefined -> erlang:error({badarg, <<\"Value cannot be null. Parameter name: \", $0/binary>>}); _ -> $1 end"
"case $1 of undefined -> erlang:error({badarg, <<\"Value cannot be null. (Parameter '\", $0/binary, \"')\">>}); _ -> $1 end"
|> Some
// Lock — no-op in Erlang (processes are isolated), just call the action
| "Lock", [ _lockObj; action ] -> CurriedApply(action, [ Value(UnitConstant, None) ], _t, r) |> Some
Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/Dart/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ let operators (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr o
| "FailWith", [ msg ]
| "InvalidOp", [ msg ] -> makeThrow r t (error com msg) |> Some
| "InvalidArg", [ argName; msg ] ->
let msg = add (add msg (str "\\nParameter name: ")) argName
let msg = add msg (add (add (str " (Parameter '") argName) (str "')"))
makeThrow r t (error com msg) |> Some
| "Raise", [ arg ] -> makeThrow r t arg |> Some
| "Reraise", _ -> Extended(Throw(None, t), r) |> Some
Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/Python/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ let operators (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr o
| "FailWith", [ msg ]
| "InvalidOp", [ msg ] -> makeThrow r t (error com msg) |> Some
| "InvalidArg", [ argName; msg ] ->
let msg = add (add msg (str "\\nParameter name: ")) argName
let msg = add msg (add (add (str " (Parameter '") argName) (str "')"))
makeThrow r t (error com msg) |> Some
| "Raise", [ arg ] -> makeThrow r t arg |> Some
| "Reraise", _ ->
Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,7 @@ let operators (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr o
| "FailWith", [ msg ]
| "InvalidOp", [ msg ] -> makeThrow r t (error com msg) |> Some
| "InvalidArg", [ argName; msg ] ->
let msg = add (add msg (str "\\nParameter name: ")) argName
let msg = add msg (add (add (str " (Parameter '") argName) (str "')"))
makeThrow r t (error com msg) |> Some
| "Raise", [ arg ] -> makeThrow r t arg |> Some
| "Reraise", _ ->
Expand Down
6 changes: 6 additions & 0 deletions tests/Beam/ExceptionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,9 @@ let ``test nested try-catch with custom exceptions`` () =
[<Fact>]
let ``test custom exception in throwsAnyError`` () =
throwsAnyError (fun () -> raise (MyError "boom"))

[<Fact>]
let ``test invalidArg formats the message like .NET`` () =
throwsError "This is invalid (Parameter 'arg')" (fun () ->
invalidArg "arg" "This is invalid"
)
5 changes: 5 additions & 0 deletions tests/Dart/src/MiscTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,8 @@ let tests() =
// throwsAnyError (fun () ->
// nullArgCheck<string> "str" null
// )

testCase "invalidArg formats the message like .NET" <| fun () ->
throwsError "This is invalid (Parameter 'arg')" (fun () ->
invalidArg "arg" "This is invalid"
)
31 changes: 31 additions & 0 deletions tests/Dart/src/Tests.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,34 @@ let testList (msg: string) (tests: unit list) = ()
let equal (expected: 'T) (actual: 'T) = Test.expect(actual, Test.equals(expected))
let notEqual (expected: 'T) (actual: 'T) = Test.expect(actual, Test.isNot(Test.equals(expected)))
let throwsAnyError (f: unit -> 'a): unit = Test.expect(f, Test.throwsException)

let private run (f: unit -> 'a) =
try
f()
|> Ok
with e ->
Error e.Message

module private RunResult =
let wasError expected actual =
match actual with
| Error _ when System.String.IsNullOrEmpty expected -> ()
| Error actual -> equal (Error expected) (Error actual)
| Ok value -> equal (Error expected) (Ok value)

let wasErrorContaining expected actual =
match actual with
| Error _ when System.String.IsNullOrEmpty expected -> ()
| Error (actual: string) when actual.Contains expected -> ()
| Error actual -> equal (Error expected) (Error actual)
| Ok value -> equal (Error expected) (Ok value)

let throwsError (expected: string) (f: unit -> 'a): unit =
run f
|> RunResult.wasError expected

/// While `throwsError` tests exception message for equality,
/// this checks if error message CONTAINS passed message
let throwsErrorContaining (expected: string) (f: unit -> 'a): unit =
run f
|> RunResult.wasErrorContaining expected
5 changes: 5 additions & 0 deletions tests/Js/Main/MiscTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,11 @@ let tests =
nullArgCheck "str" null
)

testCase "invalidArg formats the message like .NET" <| fun () ->
throwsError "This is invalid (Parameter 'arg')" (fun () ->
invalidArg "arg" "This is invalid"
)

testCase "Pattern matching optimization works (switch statement)" <| fun () ->
let mutable x = ""
let i = 4
Expand Down
6 changes: 6 additions & 0 deletions tests/Python/TestMisc.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,12 @@ let ``test nullArgCheck throws exception if argument is null`` () =
nullArgCheck "str" null
)

[<Fact>]
let ``test invalidArg formats the message like .NET`` () =
throwsError "This is invalid (Parameter 'arg')" (fun () ->
invalidArg "arg" "This is invalid"
)

# nowarn "26" // This rule will never be matched (code 26)

[<Fact>]
Expand Down