whitequark/parser — pull request #156 by yujinakayama (2014-06-22 18:15:33+00:00)
Title: Raise EncodingError for invalid encoding source

Previously, with an invalid encoding source, `ArgumentError` had been raised as a side effect of [`String#gsub` in `Source::Buffer#raw_source=`](https://github.com/whitequark/parser/blob/v2.1.9/lib/parser/source/buffer.rb#L171).

```
lib/parser/source/buffer.rb:171:in `gsub': invalid byte sequence in UTF-8 (ArgumentError)
```

This change enables users of Parser to handle the error more precisely by raising `EncodingError` instead of the too generic `ArgumentError`.

There are questions for the implementation:

* Should we create more specific error class rather than [`EncodingError`](http://www.ruby-doc.org/core-2.1.2/EncodingError.html)?
* [`Encoding::UndefinedConversionError`](http://www.ruby-doc.org/core-2.1.2/Encoding/UndefinedConversionError.html) cannot be used since the class has special attributes.
* Though I think the `ArgumentError` by `String#gsub` hasn't been considered as a public API, but this change might break [user's code that tries to rescue `ArgumentError`](https://github.com/bbatsov/rubocop/blob/v0.23.0/lib/rubocop/file_inspector.rb#L92). What do you think?