Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
March 18, 2019 is collectException working for every exceptions ? | ||||
---|---|---|---|---|
| ||||
Hello as the subject say i'm asking this question because with the following code ``` private config_create_answer create_config(string[] args) in { assert(args !is null, "args cannot be null"); assert(args.length == 2, "need 1 arguments"); } out (r) { assert(r.state == "SUCCESS", "create_config should success"); assert(!r.config_key.empty, "config_key should not be empty"); assert(!r.readonly_config_key.empty, "readonly_config_key should not be empty"); } body { string config_name; getopt(args, "name", &config_name); auto cfg = (cast(const char[])(std.file.read("../API_doc/json_recipes/config_create.json"))) .deserialize!config_create; cfg.config_name = config_name.strip("\""); client_.socket.send(cfg.serializeToJson); auto answer = new ubyte[256]; client_.socket.receive(answer); client_.socket.getErrorText.writeln; return (cast(string) answer).deserialize!config_create_answer; } unittest { import std.exception : collectException; auto cli = new CLI("/tmp/raven-os_service_albinos.sock"); assert(cli.create_config(["create_config", "--name=toto"]) .state == "SUCCESS", "should be success"); assert(cli.create_config(["create_config", "--name=\"titi\""]) .state == "SUCCESS", "should be success"); assert(collectException(cli.create_config(["create_config", "--name="]))); //here is my problem } ``` i would like to specify `collectException!GetOptException`, but it's seem's make the program exit with fail status. any idea what i'm doing wrong ? also it's my first d program, so if anything seem's bad let me know |
March 18, 2019 Re: is collectException working for every exceptions ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roman Sztergbaum | On Monday, 18 March 2019 at 18:54:22 UTC, Roman Sztergbaum wrote:
> Hello as the subject say i'm asking this question because with the following code
>
> [...]
I'm asking myself if it's usefull to write unittest with contract programming also, but this is another subject i think
|
March 18, 2019 Re: is collectException working for every exceptions ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roman Sztergbaum | On Monday, 18 March 2019 at 18:54:22 UTC, Roman Sztergbaum wrote:
> Hello as the subject say i'm asking this question because with the following code
>
> ```
> private config_create_answer create_config(string[] args)
> in
> {
> assert(args !is null, "args cannot be null");
> assert(args.length == 2, "need 1 arguments");
> }
> out (r)
> {
> assert(r.state == "SUCCESS", "create_config should success");
> assert(!r.config_key.empty, "config_key should not be empty");
> assert(!r.readonly_config_key.empty, "readonly_config_key should not be empty");
> }
> body
> {
> string config_name;
> getopt(args, "name", &config_name);
> auto cfg = (cast(const char[])(std.file.read("../API_doc/json_recipes/config_create.json")))
> .deserialize!config_create;
> cfg.config_name = config_name.strip("\"");
> client_.socket.send(cfg.serializeToJson);
> auto answer = new ubyte[256];
> client_.socket.receive(answer);
> client_.socket.getErrorText.writeln;
> return (cast(string) answer).deserialize!config_create_answer;
> }
>
> unittest
> {
> import std.exception : collectException;
>
> auto cli = new CLI("/tmp/raven-os_service_albinos.sock");
> assert(cli.create_config(["create_config", "--name=toto"])
> .state == "SUCCESS", "should be success");
> assert(cli.create_config(["create_config", "--name=\"titi\""])
> .state == "SUCCESS", "should be success");
> assert(collectException(cli.create_config(["create_config", "--name="]))); //here is my problem
> }
> ```
>
> i would like to specify `collectException!GetOptException`, but it's seem's make the program exit with fail status.
>
> any idea what i'm doing wrong ?
>
> also it's my first d program, so if anything seem's bad let me know
I haven't spot the exact position of the problem yet, but I think the usage of assert is not correct at same places of your coding.
- Assertions are for logic errors, not for issues related to resources (network, files, input from users,...)
You cannot recover from a logic error, there assert terminates the application.
- for resource issues you should throw Exceptions. You can recover from resource issues.
- assertions throwing "Errors" which are not catched by collectException.
Kind regards
Andre
|
March 18, 2019 Re: is collectException working for every exceptions ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roman Sztergbaum | On 03/18/2019 11:54 AM, Roman Sztergbaum wrote: > Hello as the subject say i'm asking this question because with the > following code Andre Pany has already explained. Otherwise, I was going to say "collectException can collect Exceptions, not exceptions." ;) There have been many discussions on the General forum on when to call assert() vs. enforce() (essentially, 'throw new Exception') and what should go inside contracts. Unrelated, with expression based contracts and the 'body' keyword now being an optional (context-dependent?) keyword, your function can be written more cleanly: private config_create_answer create_config(string[] args) in (args !is null, "args cannot be null") in (args.length == 2, "need 1 arguments") out (r; r.state == "SUCCESS", "create_config should success") out (r; !r.config_key.empty, "config_key should not be empty") out (r; !r.readonly_config_key.empty, "readonly_config_key should not be empty"); { // ... } Ali |
March 19, 2019 Re: is collectException working for every exceptions ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Tuesday, 19 March 2019 at 00:07:44 UTC, Ali Çehreli wrote:
> On 03/18/2019 11:54 AM, Roman Sztergbaum wrote:
> > [...]
> with the
> > [...]
>
> Andre Pany has already explained. Otherwise, I was going to say "collectException can collect Exceptions, not exceptions." ;)
>
> [...]
Hello,
What happen if the contract is not respected in this case ? since you dont have any assert inside the contract ?
Thank's a lot !
|
March 19, 2019 Re: is collectException working for every exceptions ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roman Sztergbaum | On 03/19/2019 12:00 AM, Roman Sztergbaum wrote: > On Tuesday, 19 March 2019 at 00:07:44 UTC, Ali Çehreli wrote: >> On 03/18/2019 11:54 AM, Roman Sztergbaum wrote: >> > [...] >> with the >> > [...] >> >> Andre Pany has already explained. Otherwise, I was going to say "collectException can collect Exceptions, not exceptions." ;) >> >> [...] > > Hello, > > What happen if the contract is not respected in this case ? since you dont have any assert inside the contract ? > > Thank's a lot ! You didn't quote that part but I think you're referring to the expression-based contracts. Actually, the asserts are still there but the syntax is cleaner. That syntax was introduced in 2.081.0: https://dlang.org/changelog/2.081.0.html#expression-based_contract_syntax I have a section about them as well: http://ddili.org/ders/d.en/contracts.html#ix_contracts.expression-based%20contracts Ali |
Copyright © 1999-2021 by the D Language Foundation