Skip to content

Commit

Permalink
Adding error handling when missing url (#3791)
Browse files Browse the repository at this point in the history
* Fixing error message when missing url

* Fixing missing url

* Adding missing url case

* Update Axios.js

* Update requests.spec.js

* Update api.spec.js

* Update api.spec.js

* Update api.spec.js

Co-authored-by: Jay <jasonsaayman@gmail.com>
  • Loading branch information
Hirotaka Tagawa / wafuwafu13 and jasonsaayman committed Dec 23, 2021
1 parent 9964815 commit 9579290
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/core/Axios.js
Expand Up @@ -36,6 +36,10 @@ Axios.prototype.request = function request(configOrUrl, config) {
config = configOrUrl || {};
}

if (!config.url) {
throw new Error('Provided config url is not valid');
}

config = mergeConfig(this.defaults, config);

// Set config.method
Expand Down Expand Up @@ -118,6 +122,9 @@ Axios.prototype.request = function request(configOrUrl, config) {
};

Axios.prototype.getUri = function getUri(config) {
if (!config.url) {
throw new Error('Provided config url is not valid');
}
config = mergeConfig(this.defaults, config);
return buildURL(config.url, config.params, config.paramsSerializer).replace(/^\?/, '');
};
Expand Down
2 changes: 1 addition & 1 deletion test/specs/api.spec.js
Expand Up @@ -11,7 +11,7 @@ describe('static api', function () {
});

it('should have promise method helpers', function () {
var promise = axios();
var promise = axios('/test');

expect(typeof promise.then).toEqual('function');
expect(typeof promise.catch).toEqual('function');
Expand Down
18 changes: 18 additions & 0 deletions test/specs/requests.spec.js
Expand Up @@ -7,6 +7,24 @@ describe('requests', function () {
jasmine.Ajax.uninstall();
});

it('should throw error when missing url', function (done) {
expect(() => axios()).toThrowError(/Provided config url is not valid/);
done();

expect(() => axios('')).toThrowError(/Provided config url is not valid/);
done();

expect(() => axios({
url: undefined,
})).toThrowError(/Provided config url is not valid/);
done();

expect(() => axios({
method: 'POST',
})).toThrowError(/Provided config url is not valid/);
done();
});

it('should treat single string arg as url', function (done) {
axios('/foo');

Expand Down

0 comments on commit 9579290

Please sign in to comment.