Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After throws unhelpful error message on passing invalid index #6415

Closed
BaibhaVatsa opened this issue Oct 11, 2019 · 6 comments · Fixed by #6421
Closed

After throws unhelpful error message on passing invalid index #6415

BaibhaVatsa opened this issue Oct 11, 2019 · 6 comments · Fixed by #6421
Labels

Comments

@BaibhaVatsa
Copy link
Contributor

Issue description

On passing negative indices, after throws error message "can't return negative/empty count of items from sequence" which does not accurately explain the error. Something like "cannot return slice of elements after a negative index" would be more explanatory in my opinion. Any suggestions?

If this gets the approval, I will be more than happy to submit a PR.

What version of Hugo are you using (hugo version)?

$ hugo version
Hugo Static Site Generator v0.59.0-DEV-096A4B67 linux/amd64 BuildDate: 2019-10-11T02:36:33-0500

Does this issue reproduce with the latest release?

Yes

@bep
Copy link
Member

bep commented Oct 11, 2019

I agree that the error isn't great. Another problem with these related "slice functions" (After, First and probably others) is that they return slightly different error messages for the same issue.

Maybe we can get some input from the Go compiler and start clean this up.

https://play.golang.org/p/P8aYI5j7lxp

package main

import (
	"fmt"
)

func main() {
	slice(-1, 2)
	slice(0, 20)
}

var s = []string{"a", "b", "c"}

func slice(a, b int) {
	defer func() {
		if r := recover(); r != nil {
			fmt.Printf("%d:%d => %s\n", a, b, r)
		}
	}()
	fmt.Println(s[a:b])
}

Which prints:

-1:2 => runtime error: slice bounds out of range [-1:]
0:20 => runtime error: slice bounds out of range [:20] with capacity 3

So for your After example, maybe:

sequence bounds out of range [-1:]

@BaibhaVatsa
Copy link
Contributor Author

I feel that the suggested error message makes more sense for after. Should I submit a PR with the change for after?

First throws "can't return negative count of items from sequence" which I feel can be changed to something like

sequence length must be non-negative

to resemble more of what the Go compiler would throw.

@bep
Copy link
Member

bep commented Oct 11, 2019

Sounds good.

@BaibhaVatsa
Copy link
Contributor Author

I was thinking that once #6419 is closed, I will change last's throw message also to

sequence length must be non-negative

as it would have the same condition as first then.

@bep
Copy link
Member

bep commented Oct 11, 2019

I agree.

BaibhaVatsa added a commit to BaibhaVatsa/hugo that referenced this issue Oct 11, 2019
Modified the messages functions after, first, and last threw on being passed invalid parameters (index or limit) to be more standardised and resemble what Go compiler would throw.

Fixes gohugoio#6415
BaibhaVatsa added a commit to BaibhaVatsa/hugo that referenced this issue Oct 11, 2019
Modified the messages functions after, first, and last threw on being passed invalid parameters (index or limit) to be more standardised and resemble what Go compiler would throw.

Fixes gohugoio#6415
@bep bep closed this as completed in #6421 Oct 12, 2019
bep pushed a commit that referenced this issue Oct 12, 2019
Modified the messages functions after, first, and last threw on being passed invalid parameters (index or limit) to be more standardised and resemble what Go compiler would throw.

Fixes #6415
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants