Giter VIP home page Giter VIP logo

Comments (5)

straight-shoota avatar straight-shoota commented on May 26, 2024 3

I suppose we could change the error message to print only the first n characters of the string?

from crystal.

philipp-kempgen avatar philipp-kempgen commented on May 26, 2024

I suppose we could change the error message to print only the first n characters of the string?

Yes. I think I would change it to something like this:

      max_length = 50
      raise JSON::ParseException.new(String.build{ |ret|
        ret << "Couldn't parse #{self} from "
        ret << string[0, max_length].inspect
        if string.size > max_length
          ret << "... (truncated, string had "
          ret << string.bytesize.format('.', '_')
          ret << " B)"
        end
      }, *location)

I'm not sure about adding .inspect though. In a way it's nicer, as you get quotes around the string, so it doesn't look weird if it's truncated. But then again the JSON string itself is harder to read.

And I'm also not sure about a reasonable max_length. Around 50 or 100 I'd say.

I did a quick test to check if any control characters in the data could mess up my terminal if I don't use .inspect. The answer is no, as the lexer would complain first.

But: IMHO .inspect should be added in JSON::Lexer, for that very reason.

raise "Unknown escape char: #{char}"

-      raise "Unknown escape char: #{char}"
+      raise "Unknown escape char: #{char.inspect}"

raise "Unexpected char '#{char}'"

-    raise "Unexpected char '#{char}'"
+    raise "Unexpected char #{char.inspect}"

from crystal.

straight-shoota avatar straight-shoota commented on May 26, 2024

Yeah, that sounds good. Let's go with some value in the range of 50-100 characters and see how it works.

I don't think the total bytesize very valuable. It's just an intermediary raw string, so I don't see much relevance for it.

.inspect makes a lot of sense to ensure proper formatting. I suppose it would be nice if we didn't need to escape quotes, but there's no trivial solution for this.

Maybe this implementation could even be upstreamed into String because I think it might be quite generally useful. String#truncate maybe? This should be a separate change though.

from crystal.

HertzDevil avatar HertzDevil commented on May 26, 2024

I wonder if it is better to use the string fragment at the error location here, not just the beginning of the string

from crystal.

straight-shoota avatar straight-shoota commented on May 26, 2024

Hm, good point. It's a bit more complex though because locations from the parser are line/column based and we cannot directly index into the string.

Also, I'm considering there are good options to show the start of string because it gives a clear anchor point. If you start somewhere in the middle, you don't know about context, so it could be confusing.
Perhaps we could also do a combination? Print the start of the string, then omit excess until close to the error location.

from crystal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.