-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Migrate process errors from C++ to JS #19973
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
process: migrate methods to throw errors with code
Migrate some methods from node.cc to JS in order to properly throw errors with codes.
- Loading branch information
commit 0ea59295a9d3b6f8481f67854b35fe5a64804620
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| 'use strict'; | ||
|
|
||
| const { | ||
| ERR_INVALID_ARG_TYPE, | ||
| ERR_INVALID_ARG_VALUE, | ||
| ERR_UNKNOWN_CREDENTIAL | ||
| } = require('internal/errors').codes; | ||
| const { | ||
| validateUint32 | ||
| } = require('internal/validators'); | ||
|
|
||
| function setupProcessMethods() { | ||
| const { | ||
| chdir: _chdir, | ||
| initgroups: _initgroups, | ||
| setegid: _setegid, | ||
| seteuid: _seteuid, | ||
| setgid: _setgid, | ||
| setuid: _setuid, | ||
| setgroups: _setgroups, | ||
| umask: _umask, | ||
| } = process; | ||
|
|
||
| process.chdir = chdir; | ||
| process.initgroups = initgroups; | ||
| process.setegid = setegid; | ||
| process.seteuid = seteuid; | ||
| process.setgid = setgid; | ||
| process.setuid = setuid; | ||
| process.setgroups = setgroups; | ||
| process.umask = umask; | ||
|
|
||
| function chdir(directory) { | ||
| if (typeof directory !== 'string') { | ||
| throw new ERR_INVALID_ARG_TYPE('directory', 'string', directory); | ||
| } | ||
| return _chdir(directory); | ||
| } | ||
|
|
||
| function initgroups(user, extraGroup) { | ||
| validateId(user, 'user'); | ||
| validateId(extraGroup, 'extraGroup'); | ||
| // Result is 0 on success, 1 if user is unknown, 2 if group is unknown. | ||
| const result = _initgroups(user, extraGroup); | ||
| if (result === 1) { | ||
| throw new ERR_UNKNOWN_CREDENTIAL('User', user); | ||
| } else if (result === 2) { | ||
| throw new ERR_UNKNOWN_CREDENTIAL('Group', extraGroup); | ||
| } | ||
| } | ||
|
|
||
| function setegid(id) { | ||
| return execId(id, 'Group', _setegid); | ||
| } | ||
|
|
||
| function seteuid(id) { | ||
| return execId(id, 'User', _seteuid); | ||
| } | ||
|
|
||
| function setgid(id) { | ||
| return execId(id, 'Group', _setgid); | ||
| } | ||
|
|
||
| function setuid(id) { | ||
| return execId(id, 'User', _setuid); | ||
| } | ||
|
|
||
| function setgroups(groups) { | ||
| if (!Array.isArray(groups)) { | ||
| throw new ERR_INVALID_ARG_TYPE('groups', 'Array', groups); | ||
| } | ||
| for (var i = 0; i < groups.length; i++) { | ||
| validateId(groups[i], `groups[${i}]`); | ||
| } | ||
| // Result is 0 on success. A positive integer indicates that the | ||
| // corresponding group was not found. | ||
| const result = _setgroups(groups); | ||
| if (result > 0) { | ||
| throw new ERR_UNKNOWN_CREDENTIAL('Group', groups[result - 1]); | ||
| } | ||
| } | ||
|
|
||
| const octalReg = /^[0-7]+$/; | ||
| function umask(mask) { | ||
| if (typeof mask === 'undefined') { | ||
| return _umask(mask); | ||
| } | ||
|
|
||
| if (typeof mask === 'number') { | ||
| validateUint32(mask, 'mask'); | ||
| return _umask(mask); | ||
| } | ||
|
|
||
| if (typeof mask === 'string') { | ||
| if (!octalReg.test(mask)) { | ||
| throw new ERR_INVALID_ARG_VALUE('mask', mask, | ||
| 'must be an octal string'); | ||
| } | ||
| const octal = Number.parseInt(mask, 8); | ||
| validateUint32(octal, 'mask'); | ||
| return _umask(octal); | ||
| } | ||
|
|
||
| throw new ERR_INVALID_ARG_TYPE('mask', ['number', 'string', 'undefined'], | ||
| mask); | ||
| } | ||
|
|
||
| function execId(id, type, method) { | ||
| validateId(id, 'id'); | ||
| // Result is 0 on success, 1 if credential is unknown. | ||
| const result = method(id); | ||
| if (result === 1) { | ||
| throw new ERR_UNKNOWN_CREDENTIAL(type, id); | ||
| } | ||
| } | ||
|
|
||
| function validateId(id, name) { | ||
| if (typeof id === 'number') { | ||
| validateUint32(id, name); | ||
| } else if (typeof id !== 'string') { | ||
| throw new ERR_INVALID_ARG_TYPE(name, ['number', 'string'], id); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| exports.setup = setupProcessMethods; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems rather magical, can you put in a comment around these return values or use some kind of constants shared by both sides?