Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding error handling when missing url #3791

Merged
merged 10 commits into from
Dec 23, 2021
7 changes: 7 additions & 0 deletions lib/core/Axios.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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