erg-lang / erg

A statically typed language compatible with Python
http://erg-lang.org
Apache License 2.0
2.69k stars 55 forks source link

Modify error fomat #212

Closed GreasySlug closed 1 year ago

GreasySlug commented 2 years ago

Fix: #2.

Intro

The error handling code you wrote was very clear and abstract. But what I am rewriting now is not. This code is pretty dirty and you can just discard it.

Changes proposed in this PR

I want to reformat like following.

6 mut_content_arr.map!(x -> x + 1)

: `- NameError: ::mut_content_arr(: Failure) is not defined

Error[#0572]: File .\examples\array.er, line 10, in

10 telescoping_arr.push!(6)

: `- NameError: ::telescoping_arr(: Failure) is not define

Error[#0357]: File , line 1, in

1 print

: `- NameError: print is not defined

hint: exists a similar name variable: print!


## Edit

Enable colors and chars in a specified theme experimentally.

```shell
cargo run --features "unicode pretty"

Default is ANSI color and ASCII.

The above will use unicode chars and custom colors.

@mtshiba

GreasySlug commented 1 year ago

When error position is on the right side and the error description sentence is long, the whole sentence becomes longer to the right.

4 │ mut_arr = [1, 2, 3, 4, 5].into [Int!; !5]
                             -----
                                 ╰─ AttributeError: Array({1, 4, 3, 5, 2, }, 5) object has no attribute into

Specify the width to align these to the left.

4 │ mut_arr = [1, 2, 3, 4, 5].into [Int!; !5]
  ·                          -----
  · ╭────────────────────────╯
  · ╰─ AttributeError: Array({1, 4, 3, 5, 2, }, 5) object has no attribute into
C-BJ commented 1 year ago

Consider direct fix #2? @GreasySlug Let @mtshiba make some suggestions?

GreasySlug commented 1 year ago

No, color and formats only Maybe we should work on this.. I'm trying to make it easier for #2 to color and modify as needed with this format. I'm in the process of looking into several error handling libraries to do this.

C-BJ commented 1 year ago

~No, color and formats only~ Maybe we should work on this.. I'm trying to make it easier for #2 to color and modify as needed with this format. I'm in the process of looking into several error handling libraries to do this.

2 will be closed if this PR merges

image

GreasySlug commented 1 year ago

When error position is on the right side and the error description sentence is long, the whole sentence becomes longer to the right.

4 │ mut_arr = [1, 2, 3, 4, 5].into [Int!; !5]
                             -----
                                 ╰─ AttributeError: Array({1, 4, 3, 5, 2, }, 5) object has no attribute into

Specify the width to align these to the left.

4 │ mut_arr = [1, 2, 3, 4, 5].into [Int!; !5]
  ╭──────────────────────────-----
  ╰─ AttributeError: Array({1, 4, 3, 5, 2, }, 5) object has no attribute into

Same as #2 on this, we need more than one opinion. Adding a type will inevitably lengthen error description. User type may cause very long types error like Rust. Then the format can't handle it.

mtshiba commented 1 year ago

That is OK for now.

Ultimately, the error message is split into two parts: the main message and the submessage. The submessage is simple and appears directly below the code block. The main message is longer and more detailed and appears at the end.

The format will be like:

n | invalid_code
    ____________
               ╰─ sub message
ErrorKind: main message
GreasySlug commented 1 year ago

That is OK for now.

Ultimately, the error message is split into two parts: the main message and the submessage. The submessage is simple and appears directly below the code block. The main message is longer and more detailed and appears at the end.

The format will be like:

n | invalid_code
    ____________
               ╰─ sub message
ErrorKind: main message

That's good to see. How do you plan to separate error messages into sub and main messages?

mtshiba commented 1 year ago

That's good to see. How do you plan to separate error messages into sub and main messages?

I am thinking like:

#[derive(...)]]
pub struct ErrorCore {
    pub sub_messages: Vec<Submessage>,
    pub main_message: AtomicStr
    ...
}

#[derive(...)]]
pub struct Submessage {
    pub loc: Location,
    msg: AtomicStr,
}

This changes also involve displaying errors across multiple code blocks. Let's merge this PR and then open it as a separate PR.

mtshiba commented 1 year ago

Thank you so much!