Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
refactor: add detail message to error and keep reason (#1445)
Browse files Browse the repository at this point in the history
closes #1443
  • Loading branch information
alsotang authored and fengmk2 committed Mar 13, 2019
1 parent 316e722 commit be00b65
Show file tree
Hide file tree
Showing 39 changed files with 228 additions and 224 deletions.
5 changes: 3 additions & 2 deletions controllers/registry/package/deprecate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ function* deprecateVersions() {
if (!row) {
// some version not exists
this.status = 400;
const error = '[version_error] Some versions: ' + JSON.stringify(Object.keys(body.versions)) + ' not found';
this.body = {
error: 'version_error',
reason: 'Some versions: ' + JSON.stringify(Object.keys(body.versions)) + ' not found'
error,
reason: error,
};
return;
}
Expand Down
10 changes: 6 additions & 4 deletions controllers/registry/package/dist_tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ exports.set = function* () {
var pkg = yield packageService.getModule(name, version);
if (!pkg) {
this.status = 400;
const error = '[version_error] ' + name + '@' + version + ' not exists';
this.body = {
error: 'version_error',
reason: name + '@' + version + ' not exists'
error,
reason: error,
};
return;
}
Expand All @@ -67,9 +68,10 @@ exports.destroy = function* () {
var tag = this.params.tag || this.params[1];
if (tag === 'latest') {
this.status = 400;
const error = '[dist_tag_error] Can\'t not delete latest tag';
this.body = {
error: 'dist_tag_error',
reason: 'Can\'t not delete latest tag',
error,
reason: error,
};
return;
}
Expand Down
5 changes: 3 additions & 2 deletions controllers/registry/package/download_total.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ module.exports = function* downloadTotal() {
|| !range[0].match(DATE_REG)
|| !range[1].match(DATE_REG)) {
this.status = 400;
const error = '[range_error] range must be YYYY-MM-DD:YYYY-MM-DD style';
this.body = {
error: 'range_error',
reason: 'range must be YYYY-MM-DD:YYYY-MM-DD style'
error,
reason: error,
};
return;
}
Expand Down
5 changes: 3 additions & 2 deletions controllers/registry/package/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ module.exports = function* list() {
if (rows.length === 0) {
if (!this.allowSync) {
this.status = 404;
const error = '[not_found] document not found';
this.jsonp = {
error: 'not_found',
reason: 'document not found',
error,
reason: error,
};
return;
}
Expand Down
10 changes: 6 additions & 4 deletions controllers/registry/package/list_since.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ module.exports = function* listSince() {
var query = this.query;
if (query.stale !== 'update_after') {
this.status = 400;
const error = '[query_parse_error] Invalid value for `stale`.';
this.body = {
error: 'query_parse_error',
reason: 'Invalid value for `stale`.'
error,
reason: error,
};
return;
}

var startkey = Number(query.startkey);
if (!startkey) {
this.status = 400;
const error = '[query_parse_error] Invalid value for `startkey`.';
this.body = {
error: 'query_parse_error',
reason: 'Invalid value for `startkey`.'
error,
reason: error,
};
return;
}
Expand Down
44 changes: 26 additions & 18 deletions controllers/registry/package/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ module.exports = function* save(next) {
var version = Object.keys(pkg.versions || {})[0];
if (!version) {
this.status = 400;
const error = '[version_error] package.versions is empty';
this.body = {
error: 'version_error',
reason: 'package.versions is empty'
error,
reason: error,
};
return;
}
Expand All @@ -42,10 +43,11 @@ module.exports = function* save(next) {
var result = yield packageService.authMaintainer(name, username);
if (!result.isMaintainer) {
this.status = 403;
const error = '[forbidden] ' + username + ' not authorized to modify ' + name +
', please contact maintainers: ' + result.maintainers.join(', ');
this.body = {
error: 'forbidden user',
reason: username + ' not authorized to modify ' + name +
', please contact maintainers: ' + result.maintainers.join(', ')
error,
reason: error,
};
return;
}
Expand All @@ -64,9 +66,10 @@ module.exports = function* save(next) {
}

this.status = 400;
const error = '[attachment_error] package._attachments is empty';
this.body = {
error: 'attachment_error',
reason: 'package._attachments is empty'
error,
reason: error,
};
return;
}
Expand All @@ -78,9 +81,10 @@ module.exports = function* save(next) {
// should never happened in normal request
if (!maintainers) {
this.status = 400;
const error = '[maintainers_error] request body need maintainers';
this.body = {
error: 'maintainers error',
reason: 'request body need maintainers'
error,
reason: error,
};
return;
}
Expand All @@ -95,9 +99,10 @@ module.exports = function* save(next) {
});
if (m.length === 0) {
this.status = 403;
const error = '[maintainers_error] ' + username + ' does not in maintainer list';
this.body = {
error: 'maintainers error',
reason: username + ' does not in maintainer list'
error,
reason: error,
};
return;
}
Expand All @@ -112,9 +117,10 @@ module.exports = function* save(next) {

if (tags.length === 0) {
this.status = 400;
const error = '[invalid] dist-tags should not be empty';
this.body = {
error: 'invalid',
reason: 'dist-tags should not be empty'
error,
reason: error,
};
return;
}
Expand All @@ -126,9 +132,10 @@ module.exports = function* save(next) {
var shasum;
if (exists) {
this.status = 403;
const error = '[forbidden] cannot modify pre-existing version: ' + version;
this.body = {
error: 'forbidden',
reason: 'cannot modify pre-existing version: ' + version
error,
reason: error,
};
return;
}
Expand All @@ -139,10 +146,11 @@ module.exports = function* save(next) {

if (tarballBuffer.length !== attachment.length) {
this.status = 403;
const error = '[size_wrong] Attachment size ' + attachment.length
+ ' not match download size ' + tarballBuffer.length;
this.body = {
error: 'size_wrong',
reason: 'Attachment size ' + attachment.length
+ ' not match download size ' + tarballBuffer.length,
error,
reason: error,
};
return;
}
Expand Down
5 changes: 3 additions & 2 deletions controllers/registry/package/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ module.exports = function* show() {
// if not fond, sync from source registry
if (!this.allowSync) {
this.status = 404;
const error = '[not_exists] version not found: ' + version;
this.jsonp = {
error: 'not exist',
reason: 'version not found: ' + version
error,
reason: error,
};
return;
}
Expand Down
19 changes: 10 additions & 9 deletions controllers/registry/package/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,33 @@ module.exports = function* tag() {

if (!version) {
this.status = 400;
const error = '[version_missed] version not found';
this.body = {
error: 'version_missed',
reason: 'version not found'
error,
reason: error,
};
return;
}

if (!semver.valid(version)) {
this.status = 403;
var reason = util.format('setting tag %s to invalid version: %s: %s/%s',
const error = util.format('[forbidden] setting tag %s to invalid version: %s: %s/%s',
tag, version, name, tag);
this.body = {
error: 'forbidden',
reason: reason
error,
reason: error,
};
return;
}

var mod = yield packageService.getModule(name, version);
if (!mod) {
this.status = 403;
var reason = util.format('setting tag %s to unknown version: %s: %s/%s',
const error = util.format('[forbidden] setting tag %s to unknown version: %s: %s/%s',
tag, version, name, tag);
this.body = {
error: 'forbidden',
reason: reason
error,
reason: error,
};
return;
}
Expand All @@ -50,6 +51,6 @@ module.exports = function* tag() {
this.status = 201;
this.body = {
ok: true,
modified: row.gmt_modified
modified: row.gmt_modified,
};
};
10 changes: 6 additions & 4 deletions controllers/registry/package/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ function* updateMaintainers() {

if (usernames.length === 0) {
this.status = 403;
const error = '[invalid_operation] Can not remove all maintainers';
this.body = {
error: 'invalid operation',
reason: 'Can not remove all maintainers'
error,
reason: error,
};
return;
}
Expand Down Expand Up @@ -145,9 +146,10 @@ function* updateMaintainers() {
}
if (invailds.length > 0) {
this.status = 403;
const error = '[invalid] User: `' + invailds.join(', ') + '` not exists';
this.body = {
error: 'invalid user name',
reason: 'User: `' + invailds.join(', ') + '` not exists'
error,
reason: error,
};
return;
}
Expand Down
24 changes: 14 additions & 10 deletions controllers/registry/user/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ module.exports = function* addUser() {

if (!body.password || !body.name) {
this.status = 422;
const error = '[param_error] params missing, name, email or password missing';
this.body = {
error: 'paramError',
reason: 'params missing, name, email or password missing.'
error,
reason: error,
};
return;
}
Expand All @@ -56,8 +57,8 @@ module.exports = function* addUser() {
} catch (err) {
this.status = err.status || 500;
this.body = {
error: err.name,
reason: err.message
error: err.message,
reason: err.message,
};
return;
}
Expand All @@ -74,9 +75,10 @@ module.exports = function* addUser() {
if (config.customUserService) {
// user login fail, not allow to add new user
this.status = 401;
const error = '[unauthorized] Login fail, please check your login name and password';
this.body = {
error: 'unauthorized',
reason: 'Login fail, please check your login name and password'
error,
reason: error,
};
return;
}
Expand All @@ -94,19 +96,21 @@ module.exports = function* addUser() {

if (!user.salt || !user.password_sha || !user.email) {
this.status = 422;
const error = '[param_error] params missing, name, email or password missing';
this.body = {
error: 'paramError',
reason: 'params missing, name, email or password missing.'
error,
reason: error,
};
return;
}

var existUser = yield userService.get(name);
if (existUser) {
this.status = 409;
const error = '[conflict] User ' + name + ' already exists';
this.body = {
error: 'conflict',
reason: 'User ' + name + ' already exists.'
error,
reason: error,
};
return;
}
Expand Down
15 changes: 9 additions & 6 deletions controllers/registry/user/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ module.exports = function* updateUser(next) {
if (name !== this.user.name) {
// must auth user first
this.status = 401;
const error = '[unauthorized] Name is incorrect';
this.body = {
error: 'unauthorized',
reason: 'Name is incorrect.'
error,
reason: error,
};
return;
}
Expand All @@ -56,19 +57,21 @@ module.exports = function* updateUser(next) {

if (!body.password || !user.name || !user.salt || !user.password_sha || !user.email) {
this.status = 422;
const error = '[param_error] params missing, name, email or password missing';
this.body = {
error: 'paramError',
reason: 'params missing, name, email or password missing.'
error,
reason: error,
};
return;
}

var result = yield userService.update(user);
if (!result) {
this.status = 409;
const error = '[conflict] Document update conflict';
this.body = {
error: 'conflict',
reason: 'Document update conflict.'
error,
reason: error,
};
return;
}
Expand Down
Loading

0 comments on commit be00b65

Please sign in to comment.