Error -> Defect for defects (#13908)

* Error -> Defect for defects

The distinction between Error and Defect is subjective,
context-dependent and somewhat arbitrary, so when looking at an
exception, it's hard to guess what it is - this happens often when
looking at a `raises` list _without_ opening the corresponding
definition and digging through layers of inheritance.

With the help of a little consistency in naming, it's at least possible
to start disentangling the two error types and the standard lib can set
a good example here.
This commit is contained in:
Jacek Sieka
2020-04-28 19:56:01 +02:00
committed by GitHub
parent cd9af6b804
commit 7d6cbf290a
92 changed files with 323 additions and 300 deletions

View File

@@ -16,7 +16,7 @@ suite "replace":
check("123".replace(re"(?<foo>\d)(\d)", "${foo}$#$#") == "1123")
test "replacing missing captures should throw instead of segfaulting":
expect IndexError: discard "ab".replace(re"(a)|(b)", "$1$2")
expect IndexError: discard "b".replace(re"(a)?(b)", "$1$2")
expect IndexDefect: discard "ab".replace(re"(a)|(b)", "$1$2")
expect IndexDefect: discard "b".replace(re"(a)?(b)", "$1$2")
expect KeyError: discard "b".replace(re"(a)?", "${foo}")
expect KeyError: discard "b".replace(re"(?<foo>a)?", "${foo}")

View File

@@ -266,7 +266,7 @@ block: # not ready for vm because exception is compile error
var i = 32
v.setBit(i)
doAssert false
except RangeError:
except RangeDefect:
discard
except:
doAssert false

View File

@@ -43,7 +43,7 @@ block tcount:
# bubble sort
for i in low(arr)..high(arr):
for j in i+1..high(arr): # Error: unhandled exception: value out of range: 5 [RangeError]
for j in i+1..high(arr): # Error: unhandled exception: value out of range: 5 [RangeDefect]
if arr[i] > arr[j]:
let tmp = arr[i]
arr[i] = arr[j]

View File

@@ -169,7 +169,7 @@ block tsegfaults:
raise newException(ValueError, "not a crash")
except ValueError:
discard
except NilAccessError:
except NilAccessDefect:
echo "caught a crash!"
for i in 0..5:
main()
@@ -212,7 +212,7 @@ block tsplit2:
try:
discard "hello".split("")
echo "false"
except AssertionError:
except AssertionDefect:
echo "true"

View File

@@ -52,7 +52,7 @@ proc test_string_slice() =
doAssert s == "ab1234567890cdefghijklmnopqrstuvwxyz"
# bug #6223
doAssertRaises(IndexError):
doAssertRaises(IndexDefect):
discard s[0..999]
echo("OK")

View File

@@ -11,7 +11,7 @@ import macros
template rejectParse(e) =
try:
discard e
raise newException(AssertionError, "This was supposed to fail: $#!" % astToStr(e))
raise newException(AssertionDefect, "This was supposed to fail: $#!" % astToStr(e))
except ValueError: discard
proc testStrip() =

View File

@@ -626,8 +626,8 @@ suite "ttimes":
doAssert a.month.Month == cast[Month](0)
doAssert a.monthday == 0
doAssertRaises(AssertionError): discard getDayOfWeek(a.monthday, a.month, a.year)
doAssertRaises(AssertionError): discard a.toTime
doAssertRaises(AssertionDefect): discard getDayOfWeek(a.monthday, a.month, a.year)
doAssertRaises(AssertionDefect): discard a.toTime
test "inX procs":
doAssert initDuration(seconds = 1).inSeconds == 1

View File

@@ -56,7 +56,7 @@ proc defectiveRobot() =
of 3: raise newException(IOError, "I can't do that Dave.")
else: assert 2 + 2 == 5
test "unittest expect":
expect IOError, OSError, ValueError, AssertionError:
expect IOError, OSError, ValueError, AssertionDefect:
defectiveRobot()
var

View File

@@ -68,8 +68,8 @@ proc main() =
# shows that checkNotZero won't be called if a nil is found earlier in chain
doAssert ?.a.x1.checkNotZero == 0.0
# checks that a chain without nil but with an empty seq still throws IndexError
doAssertRaises(IndexError): discard ?.a2.x8[3]
# checks that a chain without nil but with an empty seq still throws IndexDefect
doAssertRaises(IndexDefect): discard ?.a2.x8[3]
# make sure no double evaluation bug
doAssert witness == 0