mirror of
https://github.com/google/go-jsonnet.git
synced 2025-08-07 14:57:24 +02:00
Support for stack trace size limit
This commit is contained in:
parent
02a4eed22d
commit
a4058fc177
@ -85,23 +85,36 @@ func (ef *ErrorFormatter) showCode(buf *bytes.Buffer, loc ast.LocationRange) {
|
||||
fmt.Fprintf(buf, "\n")
|
||||
}
|
||||
|
||||
func (ef *ErrorFormatter) frame(frame *TraceFrame, buf *bytes.Buffer) {
|
||||
// TODO(sbarzowski) tabs are probably a bad idea
|
||||
fmt.Fprintf(buf, "\t%v\t%v\n", frame.Loc.String(), frame.Name)
|
||||
if ef.pretty {
|
||||
ef.showCode(buf, frame.Loc)
|
||||
}
|
||||
}
|
||||
|
||||
func (ef *ErrorFormatter) buildStackTrace(frames []TraceFrame) string {
|
||||
// https://github.com/google/jsonnet/blob/master/core/libjsonnet.cpp#L594
|
||||
maxAbove := ef.MaxStackTraceSize / 2
|
||||
maxBelow := ef.MaxStackTraceSize - maxAbove
|
||||
var buf bytes.Buffer
|
||||
for i := len(frames) - 1; i >= 0; i-- {
|
||||
f := frames[i]
|
||||
sz := len(frames)
|
||||
for i := 0; i < sz; i++ {
|
||||
// TODO(sbarzowski) make pretty format more readable (it's already useful)
|
||||
if ef.pretty {
|
||||
fmt.Fprintf(&buf, "-------------------------------------------------\n")
|
||||
}
|
||||
// TODO(sbarzowski) tabs are probably a bad idea
|
||||
fmt.Fprintf(&buf, "\t%v\t%v\n", &f.Loc, f.Name)
|
||||
if ef.pretty {
|
||||
ef.showCode(&buf, f.Loc)
|
||||
}
|
||||
if i >= maxAbove && i < sz-maxBelow {
|
||||
if ef.pretty {
|
||||
fmt.Fprintf(&buf, "\t... (skipped %v frames)\n", sz-maxAbove-maxBelow)
|
||||
} else {
|
||||
buf.WriteString("\t...\n")
|
||||
}
|
||||
|
||||
// TODO(sbarzowski) handle max stack trace size
|
||||
// TODO(sbarzowski) I think the order of frames is reversed
|
||||
i = sz - maxBelow - 1
|
||||
} else {
|
||||
ef.frame(&frames[sz-i-1], &buf)
|
||||
}
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
@ -92,7 +92,7 @@ func TestMain(t *testing.T) {
|
||||
}
|
||||
mainTests = append(mainTests, mainTest{name: name, input: input, golden: golden})
|
||||
}
|
||||
errFormatter := ErrorFormatter{pretty: true}
|
||||
errFormatter := ErrorFormatter{pretty: true, MaxStackTraceSize: 9}
|
||||
for _, test := range mainTests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
vm := MakeVM()
|
||||
@ -207,7 +207,7 @@ var minimalErrorTests = []errorFormattingTest{
|
||||
}
|
||||
|
||||
func TestMinimalError(t *testing.T) {
|
||||
formatter := ErrorFormatter{}
|
||||
formatter := ErrorFormatter{MaxStackTraceSize: 20}
|
||||
genericTestErrorMessage(t, minimalErrorTests, func(r RuntimeError) string {
|
||||
return formatter.format(r)
|
||||
})
|
||||
|
103
testdata/percent_format_str5.golden
vendored
103
testdata/percent_format_str5.golden
vendored
@ -18,108 +18,7 @@ RUNTIME ERROR: Not enough values to format, got 1
|
||||
<builtin> builtin function <toString>
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:557:29-74 thunk <s> from <function <format_codes_arr>>
|
||||
|
||||
format_code(val, code, tmp.fw, tmp2.prec, j2);
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:562:38-39 thunk from <thunk <s_padded> from <function <format_codes_arr>>>
|
||||
|
||||
pad_left(s, tmp.fw, " ");
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:369:36-39 thunk from <thunk from <function <pad_left>>>
|
||||
|
||||
padding(w - std.length(str), s) + str;
|
||||
|
||||
-------------------------------------------------
|
||||
<builtin> builtin function <length>
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:369:21-40 thunk from <function <pad_left>>
|
||||
|
||||
padding(w - std.length(str), s) + str;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:365:17-18 thunk from <function <padding>>
|
||||
|
||||
aux(w, "");
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:361:20-21 function <aux>
|
||||
|
||||
if w <= 0 then
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:361:20-26 function <aux>
|
||||
|
||||
if w <= 0 then
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:365:13-23 function <padding>
|
||||
|
||||
aux(w, "");
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:369:13-44 function <pad_left>
|
||||
|
||||
padding(w - std.length(str), s) + str;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:369:13-50 function <pad_left>
|
||||
|
||||
padding(w - std.length(str), s) + str;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:562:29-53 thunk <s_padded> from <function <format_codes_arr>>
|
||||
|
||||
pad_left(s, tmp.fw, " ");
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:568:65-73 thunk from <function <format_codes_arr>>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:568:61-73 thunk from <function <format_codes_arr>>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:525:60-61 thunk from <function <format_codes_arr>>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:525:60-68 thunk from <function <format_codes_arr>>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:521:21-22 function <format_codes_arr>
|
||||
|
||||
v
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:525:21-69 function <format_codes_arr>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:568:21-74 function <format_codes_arr>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:525:21-69 function <format_codes_arr>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:568:21-74 function <format_codes_arr>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
||||
|
||||
... (skipped 21 frames)
|
||||
-------------------------------------------------
|
||||
<std>:525:21-69 function <format_codes_arr>
|
||||
|
||||
|
103
testdata/percent_format_str6.golden
vendored
103
testdata/percent_format_str6.golden
vendored
@ -18,108 +18,7 @@ RUNTIME ERROR: Not enough values to format, got 1
|
||||
<builtin> builtin function <toString>
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:557:29-74 thunk <s> from <function <format_codes_arr>>
|
||||
|
||||
format_code(val, code, tmp.fw, tmp2.prec, j2);
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:562:38-39 thunk from <thunk <s_padded> from <function <format_codes_arr>>>
|
||||
|
||||
pad_left(s, tmp.fw, " ");
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:369:36-39 thunk from <thunk from <function <pad_left>>>
|
||||
|
||||
padding(w - std.length(str), s) + str;
|
||||
|
||||
-------------------------------------------------
|
||||
<builtin> builtin function <length>
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:369:21-40 thunk from <function <pad_left>>
|
||||
|
||||
padding(w - std.length(str), s) + str;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:365:17-18 thunk from <function <padding>>
|
||||
|
||||
aux(w, "");
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:361:20-21 function <aux>
|
||||
|
||||
if w <= 0 then
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:361:20-26 function <aux>
|
||||
|
||||
if w <= 0 then
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:365:13-23 function <padding>
|
||||
|
||||
aux(w, "");
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:369:13-44 function <pad_left>
|
||||
|
||||
padding(w - std.length(str), s) + str;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:369:13-50 function <pad_left>
|
||||
|
||||
padding(w - std.length(str), s) + str;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:562:29-53 thunk <s_padded> from <function <format_codes_arr>>
|
||||
|
||||
pad_left(s, tmp.fw, " ");
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:568:65-73 thunk from <function <format_codes_arr>>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:568:61-73 thunk from <function <format_codes_arr>>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:525:60-61 thunk from <function <format_codes_arr>>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:525:60-68 thunk from <function <format_codes_arr>>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:521:21-22 function <format_codes_arr>
|
||||
|
||||
v
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:525:21-69 function <format_codes_arr>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:568:21-74 function <format_codes_arr>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:525:21-69 function <format_codes_arr>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:568:21-74 function <format_codes_arr>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
||||
|
||||
... (skipped 21 frames)
|
||||
-------------------------------------------------
|
||||
<std>:525:21-69 function <format_codes_arr>
|
||||
|
||||
|
78
testdata/percent_format_str7.golden
vendored
78
testdata/percent_format_str7.golden
vendored
@ -21,83 +21,7 @@ RUNTIME ERROR: Format required number at 0, got string
|
||||
padding(w - std.length(str), s) + str;
|
||||
|
||||
-------------------------------------------------
|
||||
<builtin> builtin function <length>
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:369:21-40 thunk from <function <pad_left>>
|
||||
|
||||
padding(w - std.length(str), s) + str;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:365:17-18 thunk from <function <padding>>
|
||||
|
||||
aux(w, "");
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:361:20-21 function <aux>
|
||||
|
||||
if w <= 0 then
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:361:20-26 function <aux>
|
||||
|
||||
if w <= 0 then
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:365:13-23 function <padding>
|
||||
|
||||
aux(w, "");
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:369:13-44 function <pad_left>
|
||||
|
||||
padding(w - std.length(str), s) + str;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:369:13-50 function <pad_left>
|
||||
|
||||
padding(w - std.length(str), s) + str;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:562:29-53 thunk <s_padded> from <function <format_codes_arr>>
|
||||
|
||||
pad_left(s, tmp.fw, " ");
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:568:65-73 thunk from <function <format_codes_arr>>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:568:61-73 thunk from <function <format_codes_arr>>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:525:60-61 thunk from <function <format_codes_arr>>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:525:60-68 thunk from <function <format_codes_arr>>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:521:21-22 function <format_codes_arr>
|
||||
|
||||
v
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:525:21-69 function <format_codes_arr>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
||||
|
||||
-------------------------------------------------
|
||||
<std>:568:21-74 function <format_codes_arr>
|
||||
|
||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
||||
|
||||
... (skipped 16 frames)
|
||||
-------------------------------------------------
|
||||
<std>:525:21-69 function <format_codes_arr>
|
||||
|
||||
|
5
testdata/recursive_thunk.golden
vendored
5
testdata/recursive_thunk.golden
vendored
@ -20,10 +20,7 @@ local foo(x) = bar(foo(x - 1), x - 1);
|
||||
local bar(th, x) = if x == 0 then error "xxx" else th;
|
||||
|
||||
-------------------------------------------------
|
||||
testdata/recursive_thunk:2:16-38 function <foo>
|
||||
|
||||
local foo(x) = bar(foo(x - 1), x - 1);
|
||||
|
||||
... (skipped 1 frames)
|
||||
-------------------------------------------------
|
||||
testdata/recursive_thunk:2:20-30 thunk from <function <foo>>
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user