Skip to content

Commit

Permalink
Throw error objects instead of strings
Browse files Browse the repository at this point in the history
While JS *technically* allows code to throw anything (including
undefined/null/NaN), it's often considered bad practice.
  • Loading branch information
Gekkio committed Oct 30, 2020
1 parent 7b71596 commit 239a6bf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/passport-saml/saml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ class SAML {
});

if (!hasValidQuerySignature) {
throw 'Invalid signature';
throw new Error('Invalid signature');
}
});
} else {
Expand All @@ -906,7 +906,7 @@ class SAML {
matchingAlgo = crypto.getHashes()[i];
}
else {
throw alg + ' is not supported';
throw new Error(alg + ' is not supported');
}

const verifier = crypto.createVerify(matchingAlgo);
Expand All @@ -931,7 +931,7 @@ class SAML {
return (async () => {
const statusCode = doc.LogoutResponse.Status[0].StatusCode[0].$.Value;
if (statusCode !== "urn:oasis:names:tc:SAML:2.0:status:Success")
throw 'Bad status code: ' + statusCode;
throw new Error('Bad status code: ' + statusCode);

this.verifyIssuer(doc.LogoutResponse);
const inResponseTo = doc.LogoutResponse.$.InResponseTo;
Expand All @@ -948,9 +948,9 @@ class SAML {
const issuer = samlMessage.Issuer;
if (issuer) {
if (issuer[0]._ !== this.options.idpIssuer)
throw 'Unknown SAML issuer. Expected: ' + this.options.idpIssuer + ' Received: ' + issuer[0]._;
throw new Error('Unknown SAML issuer. Expected: ' + this.options.idpIssuer + ' Received: ' + issuer[0]._);
} else {
throw 'Missing SAML issuer';
throw new Error('Missing SAML issuer');
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ describe( 'passport-saml /', function() {
it( 'should throw an error if cert property is provided to saml constructor but is empty', function() {
should(function() {
new SAML( { cert: null } );
}).throw('Invalid property: cert must not be empty');
}).throw({ message: 'Invalid property: cert must not be empty' });
});

it( 'generateUniqueID should generate 20 char IDs', function() {
Expand Down Expand Up @@ -2415,7 +2415,7 @@ describe( 'passport-saml /', function() {
samlObj.validateRedirect(this.request, this.request.originalQuery, function(err) {
try {
should.exist(err);
err.should.eql(
err.message.should.eql(
'Unknown SAML issuer. Expected: foo Received: http://localhost:20000/saml2/idp/metadata.php'
);
done();
Expand All @@ -2441,7 +2441,7 @@ describe( 'passport-saml /', function() {
samlObj.validateRedirect(this.request, this.request.originalQuery, function(err) {
try {
should.exist(err);
err.should.eql('Invalid signature');
err.message.should.eql('Invalid signature');
done();
} catch (err2) {
done(err2);
Expand Down Expand Up @@ -2499,7 +2499,7 @@ describe( 'passport-saml /', function() {
samlObj.validateRedirect(this.request, this.request.originalQuery, function(err) {
try {
should.exist(err);
err.should.eql(
err.message.should.eql(
'Unknown SAML issuer. Expected: foo Received: http://localhost:20000/saml2/idp/metadata.php'
);
done();
Expand All @@ -2513,7 +2513,7 @@ describe( 'passport-saml /', function() {
samlObj.validateRedirect(this.request, this.request.originalQuery, function(err) {
try {
should.exist(err);
err.should.eql(
err.message.should.eql(
'Bad status code: urn:oasis:names:tc:SAML:2.0:status:Requester'
);
done();
Expand All @@ -2539,7 +2539,7 @@ describe( 'passport-saml /', function() {
samlObj.validateRedirect(this.request, this.request.originalQuery, function(err) {
try {
should.exist(err);
err.should.eql('Invalid signature');
err.message.should.eql('Invalid signature');
done();
} catch (err2) {
done(err2);
Expand Down

0 comments on commit 239a6bf

Please sign in to comment.