Skip to content

Commit ad979e3

Browse files
committed
Throw SemVerException, not Exception
1 parent af4d12b commit ad979e3

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

example/source/app.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ int main(string[] args) {
88
try {
99
a = SemVer(args[1]);
1010
b = SemVer(args[2]);
11-
} catch (Exception e) {
11+
} catch (SemVerException e) {
1212
writeln("Some errors. Check your input and try again");
1313
writeln(e.toString);
1414
return -2;

source/BrightProof.d

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
module BrightProof;
2+
/**
3+
* Exception for easy error handling
4+
*/
5+
class SemVerException : Exception {
6+
/**
7+
* Params:
8+
* msg = message
9+
* file = file, where SemVerException have been throwed
10+
* line = line number in file
11+
* next = next exception
12+
*/
13+
@safe pure nothrow this(string msg,
14+
string file = __FILE__,
15+
size_t line = __LINE__,
16+
Throwable next = null) {
17+
super(msg, file, line, next);
18+
}
19+
}
220

321
/**
422
* Main struct
@@ -17,7 +35,7 @@ struct SemVer {
1735
/**
1836
* Params:
1937
* i = input string
20-
* Throws: Exception if there is any syntax errors.
38+
* Throws: SemVerException if there is any syntax errors.
2139
*/
2240
this(string i) {
2341
import std.string : indexOf, isNumeric;
@@ -30,47 +48,47 @@ struct SemVer {
3048

3149
if((MajorDot == -1) || (MinorDot == -1)) {
3250
// If there is no 2 dots - this is not complete semver.
33-
throw new Exception("There is no major, minor or patch");
51+
throw new SemVerException("There is no major, minor or patch");
3452
} else if(MajorDot < 1) {
3553
// If first symbol is a dot, there is no Major.
36-
throw new Exception("There is no major version number");
54+
throw new SemVerException("There is no major version number");
3755
} else if((MinorDot < 1) || (MinorDot - MajorDot < 2)) {
3856
// If there is nothing between MajorDot and MinorDot.
39-
throw new Exception("There is no minor version number");
57+
throw new SemVerException("There is no minor version number");
4058
} else if(
4159
((PreReleaseStart < 1) && (i.length - MinorDot < 2)) ||
4260
((PreReleaseStart >= 0) && (PreReleaseStart - MinorDot < 2))) {
4361
// There is no Patch, if there is nothing after MinorDot.
4462
// and string end or `-`.
45-
throw new Exception("There is no patch version number");
63+
throw new SemVerException("There is no patch version number");
4664
} else if(
4765
((BuildStart < 1) && (i.length - PreReleaseStart < 2)) ||
4866
((BuildStart >= 0) && (BuildStart - PreReleaseStart < 2))) {
4967
// There is nothing in PreRelease, if nothing follow `-` .
50-
throw new Exception("There is no prerelease version string");
68+
throw new SemVerException("There is no prerelease version string");
5169
} else if(i.length - BuildStart < 2) {
5270
// There is no in Build, if string ends after `+`.
53-
throw new Exception("There is no build version string");
71+
throw new SemVerException("There is no build version string");
5472
}
5573

5674
// Now we know where Major, Minor, Patch, PreRelease, Build starts and ends.
5775
if(i[0..MajorDot].isNumeric) {
5876
Major = to!size_t(i[0..MajorDot]);
5977
} else {
60-
throw new Exception("There is a non-number characters in major");
78+
throw new SemVerException("There is a non-number characters in major");
6179
}
6280

6381
if(i[MajorDot+1..MinorDot].isNumeric) {
6482
Minor = to!size_t(i[MajorDot+1..MinorDot]);
6583
} else {
66-
throw new Exception("There is a non-number characters in minor");
84+
throw new SemVerException("There is a non-number characters in minor");
6785
}
6886

6987
if(PreReleaseStart != -1) {
7088
if(i[MinorDot+1..PreReleaseStart].isNumeric) {
7189
Patch = to!size_t(i[MinorDot+1..PreReleaseStart]);
7290
} else {
73-
throw new Exception("There is a non-number in patch");
91+
throw new SemVerException("There is a non-number in patch");
7492
}
7593
if(BuildStart != -1) {
7694
PreRelease = i[PreReleaseStart+1..BuildStart];
@@ -83,14 +101,14 @@ struct SemVer {
83101
if(i[MinorDot+1..BuildStart].isNumeric) {
84102
Patch = to!size_t(i[MinorDot+1..BuildStart]);
85103
} else {
86-
throw new Exception("There is a non-number in patch");
104+
throw new SemVerException("There is a non-number in patch");
87105
}
88106
Build = i[BuildStart+1..$];
89107
} else {
90108
if(i[MinorDot+1..$].isNumeric) {
91109
Patch = to!size_t(i[MinorDot+1..$]);
92110
} else {
93-
throw new Exception("There is a non-number in patch");
111+
throw new SemVerException("There is a non-number in patch");
94112
}
95113
}
96114
}

test/source/app.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void validationTest() {
4545
SemVer("1.0.0+4444");
4646
SemVer("1.0.0-eyyyyup");
4747
SemVer("1.0.0-yay+build");
48-
} catch (Exception e) {
48+
} catch (SemVerException e) {
4949
error(e, __FUNCTION__);
5050
}
5151
}
@@ -59,7 +59,7 @@ void buildTest() {
5959
s.nextMinor;
6060
s.nextMinor;
6161
assert(s.toString == "35.2.0");
62-
} catch (Exception e) {
62+
} catch (SemVerException e) {
6363
error(e, __FUNCTION__);
6464
} catch (AssertError a) {
6565
error(a, __FUNCTION__);

0 commit comments

Comments
 (0)