Skip to content

Commit fef24bc

Browse files
committed
make lib-specific error classes to throw
1 parent d3ed681 commit fef24bc

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

index.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ GenericFunction.prototype.lastMatchCallResolver = function (matchingMethods, met
1818
if (matchingMethods.length >= 1) {
1919
return matchingMethods[matchingMethods.length - 1].executor.apply(methodCallContext, methodCallArgs)
2020
} else {
21-
throw new ReferenceError("call to generic function with docstring '" + this.docstring + "': no matching method found for the arguments " + methodCallArgs.toString())
21+
throw new NoMatchingMethodError(this.docstring, methodCallArgs)
2222
}
2323
}
2424

@@ -31,7 +31,7 @@ GenericFunction.prototype.warningCallResolver = function (matchingMethods, metho
3131

3232
GenericFunction.prototype.strictCallResolver = function (matchingMethods, methodCallContext, methodCallArgs) {
3333
if (matchingMethods.length > 1) {
34-
throw new RangeError("generic function does not have discreetely partitioned domain, multiple methods match the given arguments " + methodCallArgs.toString())
34+
throw new MultipleMatchingMethodsError(this.docstring, methodCallArgs)
3535
} else {
3636
return this.lastMatchCallResolver(matchingMethods, methodCallContext, methodCallArgs)
3737
}
@@ -64,4 +64,40 @@ var defgeneric = function (docstring, callResolverIdentifier) {
6464
})
6565
}
6666

67-
module.exports = defgeneric
67+
var NoMatchingMethodError = function (docstring, args, errObj) {
68+
Object.assign (
69+
this,
70+
errObj? errObj : new Error(), //for properties like stacktrace etc
71+
{
72+
name: "generic functions: no matching method error",
73+
message: "call to generic function failed, no matching method found. Please see .docstring and .args properties of this error for more details" ,
74+
docstring: docstring,
75+
offendingArguments: args
76+
}
77+
)
78+
}
79+
80+
NoMatchingMethodError.prototype = Object.create(Error.prototype)
81+
NoMatchingMethodError.prototype.constructor = NoMatchingMethodError
82+
83+
var MultipleMatchingMethodsError = function (docstring, args) {
84+
Object.assign (
85+
this,
86+
errObj? errObj : new Error(), //for properties like stacktrace etc
87+
{
88+
name: "generic functions: multiple matching methods error",
89+
message: "generic function does not have discreetely partitioned domain, multiple methods match the given arguments " ,
90+
docstring: docstring,
91+
offendingArguments: args
92+
}
93+
)
94+
}
95+
96+
MultipleMatchingMethodsError.prototype = Object.create(Error.prototype)
97+
MultipleMatchingMethodsError.prototype.constructor = MultipleMatchingMethodsError
98+
99+
module.exports = {
100+
defgeneric,
101+
NoMatchingMethodError,
102+
MultipleMatchingMethodsError
103+
}

test/test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
var assert = require("assert")
22
var _ = require("lodash")
3-
var defgeneric = require("../index.js")
3+
var {defgeneric, NoMatchingMethodError, MultipleMatchingMethodsError} = require("../index.js")
44
var sinon = require("sinon")
55
require("mocha-sinon")
6+
67
describe("js-multiple-dispatch", function () {
78
it("should return a function", function () {
89
var add = defgeneric("testing")

0 commit comments

Comments
 (0)