Skip to content

Commit

Permalink
Clean up error handling using new helpers from go-util
Browse files Browse the repository at this point in the history
  • Loading branch information
zenhack committed Mar 5, 2023
1 parent 43f383e commit 860f224
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 68 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/ulikunitz/xz v0.5.10
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d
golang.org/x/sys v0.1.0
zenhack.net/go/util v0.0.0-20230218002511-744d2d6d1739
zenhack.net/go/util v0.0.0-20230305222753-27582c3a8381
zenhack.net/go/vdom v0.0.0-20230302054702-c81bce9d8287
zenhack.net/go/websocket-capnp v0.0.0-20230212023810-f179b8b2c72b
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ zenhack.net/go/util v0.0.0-20230216234122-5622a28960db h1:UALBg+bDj/Xqcwg2bhc6L4
zenhack.net/go/util v0.0.0-20230216234122-5622a28960db/go.mod h1:0lafdGg7tDb7RcXASgmJmRbLFLkAxu328+KGIs7icDE=
zenhack.net/go/util v0.0.0-20230218002511-744d2d6d1739 h1:/QnbZBURrZUFvnxB4wDyRrPsWzh2KWbJ6AjUjohCHJ8=
zenhack.net/go/util v0.0.0-20230218002511-744d2d6d1739/go.mod h1:0lafdGg7tDb7RcXASgmJmRbLFLkAxu328+KGIs7icDE=
zenhack.net/go/util v0.0.0-20230305222753-27582c3a8381 h1:RMq4s41z4RoYcoWxtWfB53wjYMuid04b1ydiekJXJco=
zenhack.net/go/util v0.0.0-20230305222753-27582c3a8381/go.mod h1:0lafdGg7tDb7RcXASgmJmRbLFLkAxu328+KGIs7icDE=
zenhack.net/go/vdom v0.0.0-20221212051524-f5d445b862c0 h1:b47/PxX40kcz1D2glkLjSAfDAISRpytnMqOpdsAe4Rk=
zenhack.net/go/vdom v0.0.0-20221212051524-f5d445b862c0/go.mod h1:6WIfdsOxSlWalwz8i+nMyKDjinQKQFVLDhEJGvI1dWU=
zenhack.net/go/vdom v0.0.0-20230217005730-98b273ad5e92 h1:VfflWW9K1sz/QNzVTXUCyzlaS0pvdA5D2yq6vZoqu5E=
Expand Down
118 changes: 51 additions & 67 deletions internal/server/main/external-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"

"capnproto.org/go/capnp/v3"
"capnproto.org/go/capnp/v3/exc"
"zenhack.net/go/tempest/capnp/collection"
"zenhack.net/go/tempest/capnp/external"
"zenhack.net/go/tempest/internal/config"
Expand Down Expand Up @@ -150,74 +149,59 @@ func newGrainID() string {
}

func (pc pkgController) Create(ctx context.Context, p external.Package_Controller_create) error {
args := p.Args()
actionIndex := args.ActionIndex()
title, err := args.Title()
if err != nil {
return err
}

actions, err := pc.pkg.Manifest.Actions()
if err != nil {
return err
}
if actionIndex >= uint32(actions.Len()) {
return errors.New("actionIndex out of bounds")
}
grainID := newGrainID()

tx, err := pc.server.db.Begin()
if err != nil {
return exc.WrapError("Creating database transaction", err)
}
defer tx.Rollback()
accountID, err := tx.GetCredentialAccount(
pc.userSession.Credential.Type,
pc.userSession.Credential.ScopedId,
)
if err != nil {
return exc.WrapError("Getting account ID", err)
}
return exn.Try0(func(th exn.Thrower) {
args := p.Args()
actionIndex := args.ActionIndex()
title, err := args.Title()
exn.WrapThrow(th, "getting title", err)

actions, err := pc.pkg.Manifest.Actions()
exn.WrapThrow(th, "getting actions", err)
if actionIndex >= uint32(actions.Len()) {
th(errors.New("actionIndex out of bounds"))
}
grainID := newGrainID()

err = os.MkdirAll(
config.Localstatedir+"/sandstorm/grains/"+grainID+"/sandbox",
0770,
)
if err != nil {
return exc.WrapError("Creating grain sandbox directory", err)
}
tx, err := pc.server.db.Begin()
exn.WrapThrow(th, "creating database transaction", err)

err = tx.AddGrain(database.NewGrain{
GrainId: grainID,
PkgId: pc.pkg.Id,
Title: title,
OwnerId: accountID,
defer tx.Rollback()
accountID, err := tx.GetCredentialAccount(
pc.userSession.Credential.Type,
pc.userSession.Credential.ScopedId,
)
exn.WrapThrow(th, "getting account id", err)

err = os.MkdirAll(
config.Localstatedir+"/sandstorm/grains/"+grainID+"/sandbox",
0770,
)
exn.WrapThrow(th, "creating grain sandbox directory", err)
err = tx.AddGrain(database.NewGrain{
GrainId: grainID,
PkgId: pc.pkg.Id,
Title: title,
OwnerId: accountID,
})
exn.WrapThrow(th, "creating grain in database", err)

results, err := p.AllocResults()
th(err)

results.SetId(grainID)
g, err := results.NewGrain()
th(err)
th(g.SetTitle(title))
// FIXME: Start the grain with the right action; as is this will just get launched w/
// continue when it hits the UI. Will still work for many apps.
sessionToken, err := session.GrainSession{
GrainId: grainID,
SessionId: pc.userSession.SessionId,
}.Seal(pc.sessionStore)
exn.WrapThrow(th, "creating grain session token", err)
th(g.SetSessionToken(sessionToken))
// TODO: set Handle.
exn.WrapThrow(th, "commiting database transaction", tx.Commit())
})
if err != nil {
return exc.WrapError("Creating grain in database", err)
}

results, err := p.AllocResults()
if err != nil {
return err
}
results.SetId(grainID)
g, err := results.NewGrain()
if err != nil {
return err
}
g.SetTitle(title)
// FIXME: Start the grain with the right action; as is this will just get launched w/
// continue when it hits the UI. Will still work for many apps.
sessionToken, err := session.GrainSession{
GrainId: grainID,
SessionId: pc.userSession.SessionId,
}.Seal(pc.sessionStore)
if err != nil {
return err
}
g.SetSessionToken(sessionToken)
// TODO: set Handle.
return tx.Commit()

}

0 comments on commit 860f224

Please sign in to comment.