Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

(Re)Introduce Throwable.message #1895

Merged
merged 2 commits into from Aug 31, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/object.d
Expand Up @@ -1771,6 +1771,19 @@ class Throwable : Object
}
}
}

/**
* Get the message describing the error.
* Base behavior is to return the `Throwable.msg` field.
* Override to return some other error message.
*
* Returns:
* Error message
*/
@__future const(char)[] message() const
{
return this.msg;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a test case to turn the red to green.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see all tests in green, or do you mean the green of a review approval?

Also, there are some test cases already included, is there anything in particular you see missing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 1745 above is red. It isn't executed because all the test cases override Throwable.message(). Need a test case that does not override it, and just calls it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leandro-lucarella-sociomantic There is a CodeCov browser extension, which overlays the unittest coverage on top of the diff for pull requests. I think what Walter is saying is that this line doesn't show up as covered by a unittest.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZombineDev is right. Here it is: https://github.com/codecov/browser-extension Please install it!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, had no idea there's an extension, will do it today!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've started looking into this and it feels something is off with the code coverage - because this is already covered by: https://github.com/dlang/druntime/pull/1895/files#diff-8289be999021da628af8f313e9992573R4 (I even confirmed this with printouts in the line - it sure gets triggered)

Could it be that source of the problem is that code coverage doesn't collect ones from the test suite? IIRC, we have the similar problem in sociomantic's ocean, about the code coverage overriding each other, I wonder if it same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the unittest, let's see if that helps

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, green now!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[..] it feels something is off with the code coverage - because this is already covered [..]

Not sure if there's a bugzilla issue about this (if not there should be), the reason for this initially not appearing covered is that coverage in external test files is essentially ignored. I think it is a build-system level issue, though I haven't looked in detail.

}
}


Expand Down Expand Up @@ -1827,6 +1840,11 @@ unittest
assert(e.next !is null);
assert(e.msg == "msg");
}

{
auto e = new Exception("message");
assert(e.message == "message");
}
}


Expand Down
4 changes: 3 additions & 1 deletion test/exceptions/Makefile
@@ -1,6 +1,7 @@
include ../common.mak

TESTS:=stderr_msg unittest_assert invalid_memory_operation unknown_gc static_dtor chain
TESTS:=stderr_msg unittest_assert invalid_memory_operation unknown_gc static_dtor future_message

ifeq ($(OS)-$(BUILD),linux-debug)
TESTS:=$(TESTS) line_trace rt_trap_exceptions
endif
Expand Down Expand Up @@ -33,6 +34,7 @@ $(ROOT)/unittest_assert.done: STDERR_EXP="unittest_assert msg"
$(ROOT)/invalid_memory_operation.done: STDERR_EXP="InvalidMemoryOperationError"
$(ROOT)/unknown_gc.done: STDERR_EXP="'unknowngc'"
$(ROOT)/static_dtor.done: STDERR_EXP="dtor_called_more_than_once"
$(ROOT)/future_message.done: STDERR_EXP="exception I have a custom message. exception exception "
$(ROOT)/static_dtor.done: NEGATE=!
$(ROOT)/rt_trap_exceptions.done: STDERR_EXP="uncaught exception\nobject.Exception@rt_trap_exceptions.d(11): exception"
$(ROOT)/rt_trap_exceptions.done: NEGATE=!
Expand Down
70 changes: 70 additions & 0 deletions test/exceptions/src/future_message.d
@@ -0,0 +1,70 @@
import core.stdc.stdio;

// Make sure basic stuff works with future Throwable.message
class NoMessage : Throwable
{
@nogc @safe pure nothrow this(string msg, Throwable next = null)
{
super(msg, next);
}
}

class WithMessage : Throwable
{
@nogc @safe pure nothrow this(string msg, Throwable next = null)
{
super(msg, next);
}

override const(char)[] message() const
{
return "I have a custom message.";
}
}

class WithMessageNoOverride : Throwable
{
@nogc @safe pure nothrow this(string msg, Throwable next = null)
{
super(msg, next);
}

const(char)[] message() const
{
return "I have a custom message and no override.";
}
}

class WithMessageNoOverrideAndDifferentSignature : Throwable
{
@nogc @safe pure nothrow this(string msg, Throwable next = null)
{
super(msg, next);
}

immutable(char)[] message()
{
return "I have a custom message and I'm nothing like Throwable.message.";
}
}

void test(Throwable t)
{
try
{
throw t;
}
catch (Throwable e)
{
fprintf(stderr, "%.*s ", e.message.length, e.message.ptr);
}
}

void main()
{
test(new NoMessage("exception"));
test(new WithMessage("exception"));
test(new WithMessageNoOverride("exception"));
test(new WithMessageNoOverrideAndDifferentSignature("exception"));
fprintf(stderr, "\n");
}