Skip to content

Commit

Permalink
Merge pull request #3056 from CyberShadow/pull-20150315-193827-regist…
Browse files Browse the repository at this point in the history
…ry-exception

Better std.windows.registry exceptions
  • Loading branch information
DmitryOlshansky committed Jun 11, 2015
2 parents d9f4482 + 3024e3d commit 154dac3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
47 changes: 23 additions & 24 deletions std/windows/registry.d
Expand Up @@ -64,47 +64,46 @@ private

/* ************* Exceptions *************** */

/**
*/
class Win32Exception : Exception
// Do not use. Left for compatibility.
class Win32Exception : WindowsException
{
int error;

@safe pure nothrow
@safe
this(string message, string fn = __FILE__, size_t ln = __LINE__, Throwable next = null)
{
super(message, fn, ln, next);
super(0, message, fn, ln);
}

@safe pure
@safe
this(string message, int errnum, string fn = __FILE__, size_t ln = __LINE__, Throwable next = null)
{
super(text(message, " (", errnum, ")"), fn, ln, next);
error = errnum;
super(errnum, message, fn, ln);
}

@property int error() { return super.code; }
}

unittest {
version(unittest) import std.string : startsWith, endsWith;

unittest
{
// Test that we can throw and catch one by its own type
string message = "Test W1";

auto e = collectException!Win32Exception(
enforce(false, new Win32Exception(message)));
assert(e.msg == message);
assert(e.msg.startsWith(message));
}

unittest {
unittest
{
// ditto
string message = "Test W2";
int code = 5;

auto e = collectException!Win32Exception(
enforce(false, new Win32Exception(message, code)));
assert(e.error == code);

// CAUTION: this test is to be removed in D1
// because e.msg does not contains the (code) section.
assert(e.msg == text(message, " (", code, ")"));
assert(e.msg.startsWith(message));
}

/**
Expand All @@ -120,7 +119,7 @@ public:
Params:
message = The message associated with the exception.
*/
@safe pure
@safe
this(string message, string fn = __FILE__, size_t ln = __LINE__, Throwable next = null)
{
super(message, fn, ln, next);
Expand All @@ -133,7 +132,7 @@ public:
message = The message associated with the exception.
error = The Win32 error number associated with the exception.
*/
@safe pure
@safe
this(string message, int error, string fn = __FILE__, size_t ln = __LINE__, Throwable next = null)
{
super(message, error, fn, ln, next);
Expand All @@ -149,10 +148,7 @@ unittest
auto e = collectException!RegistryException(
enforce(false, new RegistryException(message, code)));
assert(e.error == code);

// CAUTION: this test is to be removed in D1
// because e.msg does not contains the (code) section.
assert(e.msg == text(message, " (", code, ")"));
assert(e.msg.startsWith(message));
}

unittest
Expand All @@ -162,7 +158,7 @@ unittest

auto e = collectException!RegistryException(
enforce(false, new RegistryException(message)));
assert(e.msg == message);
assert(e.msg.startsWith(message));
}

/* ************* public enumerations *************** */
Expand Down Expand Up @@ -1845,4 +1841,7 @@ unittest
unittestKey.deleteKey(stateKey.name);
unittestKey.deleteKey(cityKey.name);
HKCU.deleteKey(unittestKeyName);

auto e = collectException!RegistryException(HKCU.getKey("cDhmxsX9K23a8Uf869uB"));
assert(e.msg.endsWith(" (error 2)"));
}
19 changes: 15 additions & 4 deletions std/windows/syserror.d
Expand Up @@ -42,7 +42,7 @@ version (StdDdoc)
{
private alias DWORD = int;
final @property DWORD code(); /// $(D GetLastError)'s return value.
@disable this(int dummy);
this(DWORD code, string str=null, string file = null, size_t line = 0) @trusted;
}

/++
Expand Down Expand Up @@ -130,11 +130,15 @@ class WindowsException : Exception
if (str != null)
{
buf.put(str);
buf.put(": ");
if (code)
buf.put(": ");
}

auto success = putSysError(code, buf);
formattedWrite(buf, success ? " (error %d)" : "Error %d", code);
if (code)
{
auto success = putSysError(code, buf);
formattedWrite(buf, success ? " (error %d)" : "Error %d", code);
}

super(buf.data, file, line);
}
Expand Down Expand Up @@ -163,4 +167,11 @@ unittest
assert(e.msg.startsWith("DeleteFile: "));
// can't test the entire message, as it depends on Windows locale
assert(e.msg.endsWith(" (error 2)"));

// Test code zero
e = new WindowsException(0);
assert(e.msg == "");

e = new WindowsException(0, "Test");
assert(e.msg == "Test");
}

0 comments on commit 154dac3

Please sign in to comment.