Skip to content

Commit

Permalink
fix: give more descriptive error for accountSid init issues (#828)
Browse files Browse the repository at this point in the history
To better illustrate that an Account SID is required when using an API key to init the client.

Fixes #600
  • Loading branch information
childish-sambino authored Nov 28, 2022
1 parent 3452c03 commit e18440d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
],
"quotes": [
2,
"single"
"double"
],
"indent": [
2,
Expand Down
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"newcap": true,

// Enforce use of single quotation marks for strings.
"quotmark": "single",
"quotmark": "double",

// Enforce placing 'use strict' at the top function scope
"strict": true,
Expand Down
6 changes: 5 additions & 1 deletion lib/base/BaseTwilio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ export class BaseTwilio {
}

if (!this.accountSid?.startsWith("AC")) {
throw new Error("accountSid must start with AC");
const apiKeyMsg = this.accountSid.startsWith("SK")
? ". The given SID indicates an API Key which requires the accountSid to be passed as an additional option"
: "";

throw new Error("accountSid must start with AC" + apiKeyMsg);
}
}

Expand Down
39 changes: 30 additions & 9 deletions spec/unit/rest/Twilio.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
"use strict";
const nock = require("nock");
const util = require("util");
var moduleInfo = require("../../../package.json");
var os = require("os");
var url = require("url"); /* jshint ignore:line */

describe("client", () => {
var client;
const twilio = require("../../../lib");
let client;
const Twilio = require("../../../lib");

describe("initializing", () => {
it("should use the first arg for the username as well", () => {
client = new Twilio("ACXXXXXXXX", "test-password");
expect(client.username).toEqual("ACXXXXXXXX");
expect(client.accountSid).toEqual("ACXXXXXXXX");
});

it("should use the first arg for the username as well and the option as the accountSid", () => {
client = new Twilio("SKXXXXXXXX", "test-password", {
accountSid: "ACXXXXXXXX",
});
expect(client.username).toEqual("SKXXXXXXXX");
expect(client.accountSid).toEqual("ACXXXXXXXX");
});

it("should throw given an invalid accountSid", () => {
expect(() => new Twilio("ADXXXXXXXX", "test-password")).toThrow(
"must start with"
);
expect(() => new Twilio("SKXXXXXXXX", "test-password")).toThrow(
"API Key"
);
});
});

describe("setting region and edge", () => {
beforeEach(() => {
client = new twilio("ACXXXXXXXX", "test-password");
client = new Twilio("ACXXXXXXXX", "test-password");
});
describe("setting the region", () => {
it("should use no region or edge by default", () => {
Expand Down Expand Up @@ -112,7 +133,7 @@ describe("client", () => {

describe("adding user agent extensions", () => {
it("sets the user-agent by default", () => {
const client = new twilio("ACXXXXXXXX", "test-password");
const client = new Twilio("ACXXXXXXXX", "test-password");
const scope = nock("https://api.twilio.com", {
reqheaders: {
"User-Agent":
Expand All @@ -127,7 +148,7 @@ describe("client", () => {
});

it("allows for user-agent extensions", () => {
const client = new twilio("ACXXXXXXXX", "test-password", {
const client = new Twilio("ACXXXXXXXX", "test-password", {
userAgentExtensions: [
"twilio-run/2.0.0-test",
"@twilio-labs/plugin-serverless/1.1.0-test",
Expand Down

0 comments on commit e18440d

Please sign in to comment.