expectErr

Unwraps a result, yielding the content of an error value. If there is none, or success value, it throws assert(0) with the provided message.

  1. E expectErr(auto ref EX res, lazy string msg)
    @safe
    E
    expectErr
    (
    EX : Expected!(T, E, H)
    T
    E
    H
    )
    (
    auto ref EX res
    ,
    lazy string msg
    )
  2. E expectErr(auto ref EX res)

Parameters

res
Type: EX

Expected to check the result of

msg
Type: string

message to use with assert

Examples

1 assert(err("foo").expectErr("oops") == "foo");
2 version (D_Exceptions)
3 {
4     assert(collectExceptionMsg!Throwable(Expected!int.init.expectErr("oops")) == "oops: empty");
5     assert(collectExceptionMsg!Throwable(ok(42).expectErr("oops")) == "oops: 42");
6     assert(collectExceptionMsg!Throwable(ok().expectErr("oops")) == "oops: empty"); // void value
7 }
8 
9 assert(ok("foo").expect!(a => "bar") == "foo");
10 assert(err!string("foo").expect!(a => "bar") == "bar");
11 assert(err!string("foo").expect!((a) {}) is null);
12 
13 assert(ok("foo").expectErr!(a => "bar") == "bar");
14 assert(err!string("foo").expectErr!(a => "bar") == "foo");
15 assert(ok!string("foo").expectErr!((a) {}) is null);

Meta