Skip to content

Commit

Permalink
Nodejs: correctly put Errno errors inside their ns
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Oct 16, 2017
1 parent 37ad667 commit c98322f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
26 changes: 17 additions & 9 deletions stdlib/nodejs/file.rb
@@ -1,5 +1,12 @@
%x{
var warnings = {};
var warnings = {}, errno_code, errno_codes = [
'EACCES',
'EISDIR',
'EMFILE',
'ENOENT',
'EPERM'
];
function handle_unsupported_feature(message) {
switch (Opal.config.unsupported_features_severity) {
case 'error':
Expand All @@ -23,19 +30,20 @@
try {
return action();
} catch (error) {
if (error.code === 'EACCES' ||
error.code === 'EISDIR' ||
error.code === 'EMFILE' ||
error.code === 'ENOENT' ||
error.code === 'EPERM') {
var error_class = #{Errno.const_defined?(`error.code`)} ||
#{Errno.const_set(`error.code`, Class.new(SystemCallError))}
if (errno_codes.indexOf(error.code) >= 0) {
var error_class = #{Errno.const_get(`error.code`)}
throw #{`error_class`.new(`error.message`)};
}
throw error;
}
}
for(var i = 0, ii = errno_codes.length; i < ii; i++) {
errno_code = errno_codes[i];
if (!#{Errno.const_defined?(`errno_code`)}) {
#{Errno.const_set(`errno_code`, Class.new(SystemCallError))}
}
}
}

class File < IO
Expand Down
8 changes: 3 additions & 5 deletions test/nodejs/test_file.rb
Expand Up @@ -29,7 +29,7 @@ def test_mtime
assert_kind_of(Time, t1)
assert_kind_of(Time, t2)
assert_equal(t1, t2)
File.mtime('nofile')
assert_raise(Errno::ENOENT) { File.mtime('nofile') }
end

def test_write_read
Expand Down Expand Up @@ -57,15 +57,13 @@ def test_read_each_line
end

def test_read_noexistent_should_raise_io_error
assert_raise IOError do
assert_raise Errno::ENOENT do
File.read('tmp/nonexistent')
end
end

def test_mtime_noexistent_should_raise_io_error
assert_raise IOError do
File.mtime('tmp/nonexistent')
end
assert_raise(Errno::ENOENT) { File.mtime('tmp/nonexistent') }
end

def test_current_directory_should_be_a_directory
Expand Down

0 comments on commit c98322f

Please sign in to comment.