error handling

This commit is contained in:
Gavin Nishizawa 2023-09-21 17:58:55 -07:00
parent 593bea6982
commit 8bb1e3c473
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
2 changed files with 21 additions and 13 deletions

View file

@ -68,7 +68,7 @@ func SaveOrder(g *d2graph.Graph) (restoreOrder func()) {
} }
} }
func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, coreLayout d2graph.LayoutGraph) geo.Spacing { func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, coreLayout d2graph.LayoutGraph) error {
g.Root.Box = &geo.Box{} g.Root.Box = &geo.Box{}
log.Warn(ctx, "ln info", slog.F("gi", graphInfo), slog.F("root level", g.RootLevel)) log.Warn(ctx, "ln info", slog.F("gi", graphInfo), slog.F("root level", g.RootLevel))
@ -96,7 +96,10 @@ func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, co
if isGridCellContainer { if isGridCellContainer {
nestedGraph := ExtractSubgraph(curr, true) nestedGraph := ExtractSubgraph(curr, true)
LayoutNested(ctx, nestedGraph, GraphInfo{}, coreLayout) err := LayoutNested(ctx, nestedGraph, GraphInfo{}, coreLayout)
if err != nil {
return err
}
InjectNested(g.Root, nestedGraph, false) InjectNested(g.Root, nestedGraph, false)
restoreOrder() restoreOrder()
dx := -curr.TopLeft.X dx := -curr.TopLeft.X
@ -131,12 +134,15 @@ func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, co
curr.NearKey = nil curr.NearKey = nil
} }
spacing := LayoutNested(ctx, nestedGraph, nestedInfo, coreLayout) err := LayoutNested(ctx, nestedGraph, nestedInfo, coreLayout)
if err != nil {
return err
}
if gi.IsConstantNear { if gi.IsConstantNear {
curr.NearKey = nearKey curr.NearKey = nearKey
} else { } else {
FitToGraph(curr, nestedGraph, spacing) FitToGraph(curr, nestedGraph, geo.Spacing{})
curr.TopLeft = geo.NewPoint(0, 0) curr.TopLeft = geo.NewPoint(0, 0)
} }
@ -154,32 +160,31 @@ func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, co
// We can now run layout with accurate sizes of nested layout containers // We can now run layout with accurate sizes of nested layout containers
// Layout according to the type of diagram // Layout according to the type of diagram
LayoutDiagram := func(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, coreLayout d2graph.LayoutGraph) geo.Spacing { LayoutDiagram := func(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, coreLayout d2graph.LayoutGraph) error {
spacing := geo.Spacing{}
var err error var err error
switch graphInfo.DiagramType { switch graphInfo.DiagramType {
case GridDiagram: case GridDiagram:
log.Warn(ctx, "layout grid", slog.F("rootlevel", g.RootLevel), slog.F("shapes", g.PrintString())) log.Warn(ctx, "layout grid", slog.F("rootlevel", g.RootLevel), slog.F("shapes", g.PrintString()))
if err = d2grid.Layout2(ctx, g); err != nil { if err = d2grid.Layout2(ctx, g); err != nil {
panic(err) return err
} }
case SequenceDiagram: case SequenceDiagram:
log.Warn(ctx, "layout sequence", slog.F("rootlevel", g.RootLevel), slog.F("shapes", g.PrintString())) log.Warn(ctx, "layout sequence", slog.F("rootlevel", g.RootLevel), slog.F("shapes", g.PrintString()))
err = d2sequence.Layout2(ctx, g, coreLayout) err = d2sequence.Layout2(ctx, g, coreLayout)
if err != nil { if err != nil {
panic(err) return err
} }
default: default:
log.Warn(ctx, "default layout", slog.F("rootlevel", g.RootLevel), slog.F("shapes", g.PrintString())) log.Warn(ctx, "default layout", slog.F("rootlevel", g.RootLevel), slog.F("shapes", g.PrintString()))
err := coreLayout(ctx, g) err := coreLayout(ctx, g)
if err != nil { if err != nil {
panic(err) return err
} }
} }
return spacing return nil
} }
spacing := LayoutDiagram(ctx, g, graphInfo, coreLayout) err := LayoutDiagram(ctx, g, graphInfo, coreLayout)
if len(constantNears) > 0 { if len(constantNears) > 0 {
err := d2near.Layout(ctx, g, constantNears) err := d2near.Layout(ctx, g, constantNears)
@ -195,7 +200,7 @@ func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, co
} }
log.Warn(ctx, "done", slog.F("rootlevel", g.RootLevel)) log.Warn(ctx, "done", slog.F("rootlevel", g.RootLevel))
return spacing return err
} }
func NestedGraphInfo(obj *d2graph.Object) (gi GraphInfo) { func NestedGraphInfo(obj *d2graph.Object) (gi GraphInfo) {

View file

@ -86,7 +86,10 @@ func compile(ctx context.Context, g *d2graph.Graph, compileOpts *CompileOptions,
} }
graphInfo := d2layouts.NestedGraphInfo(g.Root) graphInfo := d2layouts.NestedGraphInfo(g.Root)
d2layouts.LayoutNested(ctx, g, graphInfo, coreLayout) err = d2layouts.LayoutNested(ctx, g, graphInfo, coreLayout)
if err != nil {
return nil, err
}
if false { if false {
constantNearGraphs := d2near.WithoutConstantNears(ctx, g) constantNearGraphs := d2near.WithoutConstantNears(ctx, g)