Skip to content

Add handling exceptions fro fs.saveAs(). If file path is too large, it crashes the current execution #79

Open
@michaelhidalgo

Description

@michaelhidalgo

When fs.writeFileSync () is invoked with a non-legal path ( for example a path longer than the underlying platform path limit, which might be different in each operating system), it throws an error ENAMETOOLONG, name too long.

This exception crashes the current execution of the code.

 it.only 'Path too long unhandled exception', ->
    content  = (20).random_Letters();
    tempFile = './src' + (20000).random_Letters()
    try
      content.saveAs(tempFile)
    catch error
      #Identifying error code
      error.code.assert_Is('ENAMETOOLONG')
      error.toString().assert_Contains('Error: ENAMETOOLONG, name too long')

There are two ways to fix this :

  1. Adding a try-catch block when fs.writeFileSync () is invoked and log the error.
  2. Add a check for the path before invoking the function.

Option 2 seems to be tricky since we would need to know,somehow, what is the path limit of the underlying operating system, then we need to do an input validation for the path to see if it is not longer than the limit.
According to the documentation fs.writeFileSync () does not return nothing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions