Comments (5)
I suppose we could change the error message to print only the first n
characters of the string?
from crystal.
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.
Line 207 in f0bd16a
- raise "Unknown escape char: #{char}"
+ raise "Unknown escape char: #{char.inspect}"
Line 327 in f0bd16a
- raise "Unexpected char '#{char}'"
+ raise "Unexpected char #{char.inspect}"
from crystal.
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.
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.
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)
- SIGBUS on AArch64 when `Proc` returns large extern struct by value
- Can't initialize a struct inside `#pre_initialize` HOT 4
- Unable to run interpreter after building HOT 2
- Using `lld-link` as a linker HOT 1
- Interpreter bug when `require "http/server"` HOT 1
- Can't instantiate a UNIXSocket instance in the interpreter HOT 2
- Some `#[]?(Range)` methods can still raise `IndexError`
- Interpreter hits Invalid memory access (after Error: BUG: no target defs) HOT 1
- Multiplication of Int64 and Int32 may result in Int32 (risking overflows at runtime) HOT 4
- `Number#humanize` and `Int#humanize_bytes` should separate number and unit by a space HOT 1
- Regression: Interrupted system call in CI HOT 6
- Is an html strip-tags method a worth stdlib addition? HOT 7
- Unwinding is slow on WSL HOT 8
- add something like ruby un.rb or cmake -E for cross platform scripts HOT 3
- regression: XPath swallows errors and raises opaque `error in [...] expression` instead HOT 1
- Proposal: Utility method for string truncation HOT 2
- Debugging Windows binaries with LLDB doesn't break on access violation
- Manage compiler dependencies with `shards` HOT 6
- Passing `Proc`s with non-default calling conventions across libs HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crystal.