New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert Exceptions to Throwables #28
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module concurrency.error; | ||
|
||
// In order to handle Errors that get thrown on non-main threads we have to make a clone. There are cases where the Error is constructed in TLS, which either might be overwritten with another Error (if the thread continues work and hits another Error), or might point to invalid memory once the thread is cleaned up. In order to be safe we have to clone the Error. | ||
|
||
class ThrowableClone(T : Throwable) : T { | ||
this(Args...)(Throwable.TraceInfo info, Args args) @safe nothrow { | ||
super(args); | ||
if (info) | ||
this.info = new ClonedTraceInfo(info); | ||
} | ||
} | ||
|
||
// The reason it accepts a Throwable is because there might be classes that derive directly from Throwable but aren't Exceptions. We treat them as errors here. | ||
Throwable clone(Throwable t) nothrow @safe { | ||
import core.exception; | ||
if (auto a = cast(AssertError)t) | ||
return new ThrowableClone!AssertError(t.info, a.msg, a.file, a.line, a.next); | ||
if (auto r = cast(RangeError)t) | ||
return new ThrowableClone!RangeError(t.info, r.file, r.line, r.next); | ||
if (auto e = cast(Error)t) | ||
return new ThrowableClone!Error(t.info, t.msg, t.file, t.line, t.next); | ||
return new ThrowableClone!Throwable(t.info, t.msg, t.file, t.line, t.next); | ||
} | ||
|
||
class ClonedTraceInfo : Throwable.TraceInfo { | ||
string[] buf; | ||
this(Throwable.TraceInfo t) @trusted nothrow { | ||
if (t) { | ||
try { | ||
foreach (i, line; t) | ||
buf ~= line.idup(); | ||
} catch (Throwable t) { | ||
// alas... | ||
} | ||
} | ||
} | ||
override int opApply(scope int delegate(ref const(char[])) dg) const { | ||
return opApply((ref size_t, ref const(char[]) buf) => dg(buf)); | ||
} | ||
|
||
override int opApply(scope int delegate(ref size_t, ref const(char[])) dg) const { | ||
foreach(i, line; buf) { | ||
if (dg(i,line)) | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
override string toString() const { | ||
string buf; | ||
foreach ( i, line; this ) | ||
buf ~= i ? "\n" ~ line : line; | ||
return buf; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why specialize handling of
AssertError
andRangeError
only when there are sub-classes of Error such asFinalizeError
? Is it because those are the only ones with constructor parameters different fromError
's?Wouldn't it have been useful (and likely faster instead of successive calls to dynamic-cast) for
Error
(andObject
in the general case) to have an abstractdup
member that preserves typeinfo? Explicitly implemented for each sub-class, though. Just a thought.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a whole list. Then realized a lot of those where actually added in latest master only. Which made be realize I will never be able to specialize for all of them. So then I did the most useful ones only.
It would be immensely useful. But there isn't one. I don't think one will be added as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, cool. Why don't you think such a member will be added?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also https://forum.dlang.org/post/kkkagxuueclqcizrrmyh@forum.dlang.org.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's also the question of dup vs deep dup. Maybe a deep dup be partially inferred using
.tupleof
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok with me to merge this as is, @skoppe and maybe add an issue about handling of more sub-classes of
Error
.