mirror of
1
Fork 0

[bugfix] return 400 Bad Request on more cases of malformed AS data (#2399)

This commit is contained in:
kim 2023-11-30 16:22:34 +00:00 committed by GitHub
parent 5fd2e427bb
commit eb170003b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
47 changed files with 1493 additions and 1013 deletions

6
go.mod
View File

@ -7,15 +7,15 @@ toolchain go1.21.3
require (
codeberg.org/gruf/go-bytesize v1.0.2
codeberg.org/gruf/go-byteutil v1.2.0
codeberg.org/gruf/go-cache/v3 v3.5.6
codeberg.org/gruf/go-cache/v3 v3.5.7
codeberg.org/gruf/go-debug v1.3.0
codeberg.org/gruf/go-errors/v2 v2.2.0
codeberg.org/gruf/go-errors/v2 v2.3.1
codeberg.org/gruf/go-fastcopy v1.1.2
codeberg.org/gruf/go-iotools v0.0.0-20230811115124-5d4223615a7f
codeberg.org/gruf/go-kv v1.6.4
codeberg.org/gruf/go-logger/v2 v2.2.1
codeberg.org/gruf/go-mutexes v1.3.1
codeberg.org/gruf/go-runners v1.6.1
codeberg.org/gruf/go-runners v1.6.2
codeberg.org/gruf/go-sched v1.2.3
codeberg.org/gruf/go-store/v2 v2.2.4
github.com/DmitriyVTitov/size v1.5.0

12
go.sum
View File

@ -46,13 +46,13 @@ codeberg.org/gruf/go-bytesize v1.0.2 h1:Mo+ITi+0uZ4YNSZf2ed6Qw8acOI39W4mmgE1a8ls
codeberg.org/gruf/go-bytesize v1.0.2/go.mod h1:n/GU8HzL9f3UNp/mUKyr1qVmTlj7+xacpp0OHfkvLPs=
codeberg.org/gruf/go-byteutil v1.2.0 h1:YoxkpUOoHS82BcPXfiIcWLe/YhS8QhpNUHdfuhN09QM=
codeberg.org/gruf/go-byteutil v1.2.0/go.mod h1:cWM3tgMCroSzqoBXUXMhvxTxYJp+TbCr6ioISRY5vSU=
codeberg.org/gruf/go-cache/v3 v3.5.6 h1:TJnNOuij5DF/ZK9pDB61SlYzxidRQeYjYYW3dfFSznc=
codeberg.org/gruf/go-cache/v3 v3.5.6/go.mod h1:NbsGQUgEdNFd631WSasvCHIVAaY9ovuiSeoBwtsIeDc=
codeberg.org/gruf/go-cache/v3 v3.5.7 h1:5hut49a8Wp3hdwrCEJYj6pHY2aRR1hyTmkK4+wHVYq4=
codeberg.org/gruf/go-cache/v3 v3.5.7/go.mod h1:Thahfuf3PgHSv2+1zHpvhRdX97tx1WXurVNGWpZucAM=
codeberg.org/gruf/go-debug v1.3.0 h1:PIRxQiWUFKtGOGZFdZ3Y0pqyfI0Xr87j224IYe2snZs=
codeberg.org/gruf/go-debug v1.3.0/go.mod h1:N+vSy9uJBQgpQcJUqjctvqFz7tBHJf+S/PIjLILzpLg=
codeberg.org/gruf/go-errors/v2 v2.0.0/go.mod h1:ZRhbdhvgoUA3Yw6e56kd9Ox984RrvbEFC2pOXyHDJP4=
codeberg.org/gruf/go-errors/v2 v2.2.0 h1:CxnTtR4+BqRGeBHuG/FdCKM4m3otMdfPVez6ReBebkM=
codeberg.org/gruf/go-errors/v2 v2.2.0/go.mod h1:LfzD9nkAAJpEDbkUqOZQ2jdaQ8VrK0pnR36zLOMFq6Y=
codeberg.org/gruf/go-errors/v2 v2.3.1 h1:5+OChx06R8HT+OFB3KFetPdaptQYBS9XVZKKf30wIbk=
codeberg.org/gruf/go-errors/v2 v2.3.1/go.mod h1:LfzD9nkAAJpEDbkUqOZQ2jdaQ8VrK0pnR36zLOMFq6Y=
codeberg.org/gruf/go-fastcopy v1.1.2 h1:YwmYXPsyOcRBxKEE2+w1bGAZfclHVaPijFsOVOcnNcw=
codeberg.org/gruf/go-fastcopy v1.1.2/go.mod h1:GDDYR0Cnb3U/AIfGM3983V/L+GN+vuwVMvrmVABo21s=
codeberg.org/gruf/go-fastpath/v2 v2.0.0 h1:iAS9GZahFhyWEH0KLhFEJR+txx1ZhMXxYzu2q5Qo9c0=
@ -69,8 +69,8 @@ codeberg.org/gruf/go-maps v1.0.3 h1:VDwhnnaVNUIy5O93CvkcE2IZXnMB1+IJjzfop9V12es=
codeberg.org/gruf/go-maps v1.0.3/go.mod h1:D5LNDxlC9rsDuVQVM6JObaVGAdHB6g2dTdOdkh1aXWA=
codeberg.org/gruf/go-mutexes v1.3.1 h1:8ibAjWwx08GJSq5R+lM9nwtJw2aAhMPKSXbfJ9EpDsA=
codeberg.org/gruf/go-mutexes v1.3.1/go.mod h1:1j/6/MBeBQUedAtAtysLLnBKogfOZAxdym0E3wlaBD8=
codeberg.org/gruf/go-runners v1.6.1 h1:0KNiEfGnmNUs9intqxEAWqIKUyxVOmYTtn3kPVOHsjQ=
codeberg.org/gruf/go-runners v1.6.1/go.mod h1:QRcSExqXX8DM0rm8Xs6qX7baOzyvw0JIe4mu3TsQT+Y=
codeberg.org/gruf/go-runners v1.6.2 h1:oQef9niahfHu/wch14xNxlRMP8i+ABXH1Cb9PzZ4oYo=
codeberg.org/gruf/go-runners v1.6.2/go.mod h1:Tq5PrZ/m/rBXbLZz0u5if+yP3nG5Sf6S8O/GnyEePeQ=
codeberg.org/gruf/go-sched v1.2.3 h1:H5ViDxxzOBR3uIyGBCf0eH8b1L8wMybOXcdtUUTXZHk=
codeberg.org/gruf/go-sched v1.2.3/go.mod h1:vT9uB6KWFIIwnG9vcPY2a0alYNoqdL1mSzRM8I+PK7A=
codeberg.org/gruf/go-store/v2 v2.2.4 h1:8HO1Jh2gg7boQKA3hsDAIXd9zwieu5uXwDXEcTOD9js=

View File

@ -328,29 +328,6 @@ func ExtractAttributedToURI(i WithAttributedTo) (*url.URL, error) {
return nil, gtserror.New("couldn't find iri for attributed to")
}
// ExtractPublished extracts the published time from the given
// WithPublished. Will return an error if the published property
// is not set, is not a time.Time, or is zero.
func ExtractPublished(i WithPublished) (time.Time, error) {
t := time.Time{}
publishedProp := i.GetActivityStreamsPublished()
if publishedProp == nil {
return t, gtserror.New("published prop was nil")
}
if !publishedProp.IsXMLSchemaDateTime() {
return t, gtserror.New("published prop was not date time")
}
t = publishedProp.Get()
if t.IsZero() {
return t, gtserror.New("published time was zero")
}
return t, nil
}
// ExtractIconURI extracts the first URI it can find from
// the given WithIcon which links to a supported image file.
// Input will look something like this:

View File

@ -416,6 +416,12 @@ type WithOutbox interface {
SetActivityStreamsOutbox(vocab.ActivityStreamsOutboxProperty)
}
// WithSharedInbox represents an activity with ActivityStreamsSharedInboxProperty
type WithSharedInbox interface {
GetActivityStreamsSharedInbox() vocab.ActivityStreamsSharedInboxProperty
SetActivityStreamsSharedInbox(vocab.ActivityStreamsSharedInboxProperty)
}
// WithFollowing represents an activity with ActivityStreamsFollowingProperty
type WithFollowing interface {
GetActivityStreamsFollowing() vocab.ActivityStreamsFollowingProperty

View File

@ -55,14 +55,10 @@ func MustSet[W, T any](fn func(W, T) error, with W, value T) {
// GetJSONLDId returns the ID of 'with', or nil.
func GetJSONLDId(with WithJSONLDId) *url.URL {
idProp := with.GetJSONLDId()
if idProp == nil {
if idProp == nil || !idProp.IsXMLSchemaAnyURI() {
return nil
}
id := idProp.Get()
if id == nil {
return nil
}
return id
return idProp.Get()
}
// SetJSONLDId sets the given URL to the JSONLD ID of 'with'.
@ -70,9 +66,9 @@ func SetJSONLDId(with WithJSONLDId, id *url.URL) {
idProp := with.GetJSONLDId()
if idProp == nil {
idProp = streams.NewJSONLDIdProperty()
with.SetJSONLDId(idProp)
}
idProp.SetIRI(id)
with.SetJSONLDId(idProp)
}
// SetJSONLDIdStr sets the given string to the JSONLDID of 'with'. Returns error
@ -139,14 +135,46 @@ func AppendBcc(with WithBcc, bcc ...*url.URL) {
}, bcc...)
}
// GetActor returns the IRIs contained in the Actor property of 'with'. Panics on entries with missing ID.
func GetActor(with WithActor) []*url.URL {
// GetURL returns the IRIs contained in the URL property of 'with'.
func GetURL(with WithURL) []*url.URL {
urlProp := with.GetActivityStreamsUrl()
if urlProp == nil || urlProp.Len() == 0 {
return nil
}
urls := make([]*url.URL, 0, urlProp.Len())
for i := 0; i < urlProp.Len(); i++ {
at := urlProp.At(i)
if at.IsXMLSchemaAnyURI() {
u := at.GetXMLSchemaAnyURI()
urls = append(urls, u)
}
}
return urls
}
// AppendURL appends the given URLs to the URL property of 'with'.
func AppendURL(with WithURL, url ...*url.URL) {
if len(url) == 0 {
return
}
urlProp := with.GetActivityStreamsUrl()
if urlProp == nil {
urlProp = streams.NewActivityStreamsUrlProperty()
with.SetActivityStreamsUrl(urlProp)
}
for _, u := range url {
urlProp.AppendXMLSchemaAnyURI(u)
}
}
// GetActorIRIs returns the IRIs contained in the Actor property of 'with'.
func GetActorIRIs(with WithActor) []*url.URL {
actorProp := with.GetActivityStreamsActor()
return getIRIs[vocab.ActivityStreamsActorPropertyIterator](actorProp)
}
// AppendActor appends the given IRIs to the Actor property of 'with'.
func AppendActor(with WithActor, actor ...*url.URL) {
// AppendActorIRIs appends the given IRIs to the Actor property of 'with'.
func AppendActorIRIs(with WithActor, actor ...*url.URL) {
appendIRIs(func() Property[vocab.ActivityStreamsActorPropertyIterator] {
actorProp := with.GetActivityStreamsActor()
if actorProp == nil {
@ -157,7 +185,25 @@ func AppendActor(with WithActor, actor ...*url.URL) {
}, actor...)
}
// GetAttributedTo returns the IRIs contained in the AttributedTo property of 'with'. Panics on entries with missing ID.
// GetObjectIRIs returns the IRIs contained in the Object property of 'with'.
func GetObjectIRIs(with WithObject) []*url.URL {
objectProp := with.GetActivityStreamsObject()
return getIRIs[vocab.ActivityStreamsObjectPropertyIterator](objectProp)
}
// AppendObjectIRIs appends the given IRIs to the Object property of 'with'.
func AppendObjectIRIs(with WithObject) {
appendIRIs(func() Property[vocab.ActivityStreamsObjectPropertyIterator] {
objectProp := with.GetActivityStreamsObject()
if objectProp == nil {
objectProp = streams.NewActivityStreamsObjectProperty()
with.SetActivityStreamsObject(objectProp)
}
return objectProp
})
}
// GetAttributedTo returns the IRIs contained in the AttributedTo property of 'with'.
func GetAttributedTo(with WithAttributedTo) []*url.URL {
attribProp := with.GetActivityStreamsAttributedTo()
return getIRIs[vocab.ActivityStreamsAttributedToPropertyIterator](attribProp)
@ -175,7 +221,7 @@ func AppendAttributedTo(with WithAttributedTo, attribTo ...*url.URL) {
}, attribTo...)
}
// GetInReplyTo returns the IRIs contained in the InReplyTo property of 'with'. Panics on entries with missing ID.
// GetInReplyTo returns the IRIs contained in the InReplyTo property of 'with'.
func GetInReplyTo(with WithInReplyTo) []*url.URL {
replyProp := with.GetActivityStreamsInReplyTo()
return getIRIs[vocab.ActivityStreamsInReplyToPropertyIterator](replyProp)
@ -193,10 +239,105 @@ func AppendInReplyTo(with WithInReplyTo, replyTo ...*url.URL) {
}, replyTo...)
}
// GetInbox returns the IRI contained in the Inbox property of 'with'.
func GetInbox(with WithInbox) *url.URL {
inboxProp := with.GetActivityStreamsInbox()
if inboxProp == nil || !inboxProp.IsIRI() {
return nil
}
return inboxProp.GetIRI()
}
// SetInbox sets the given IRI on the Inbox property of 'with'.
func SetInbox(with WithInbox, inbox *url.URL) {
inboxProp := with.GetActivityStreamsInbox()
if inboxProp == nil {
inboxProp = streams.NewActivityStreamsInboxProperty()
with.SetActivityStreamsInbox(inboxProp)
}
inboxProp.SetIRI(inbox)
}
// GetOutbox returns the IRI contained in the Outbox property of 'with'.
func GetOutbox(with WithOutbox) *url.URL {
outboxProp := with.GetActivityStreamsOutbox()
if outboxProp == nil || !outboxProp.IsIRI() {
return nil
}
return outboxProp.GetIRI()
}
// SetOutbox sets the given IRI on the Outbox property of 'with'.
func SetOutbox(with WithOutbox, outbox *url.URL) {
outboxProp := with.GetActivityStreamsOutbox()
if outboxProp == nil {
outboxProp = streams.NewActivityStreamsOutboxProperty()
with.SetActivityStreamsOutbox(outboxProp)
}
outboxProp.SetIRI(outbox)
}
// GetFollowers returns the IRI contained in the Following property of 'with'.
func GetFollowing(with WithFollowing) *url.URL {
followProp := with.GetActivityStreamsFollowing()
if followProp == nil || !followProp.IsIRI() {
return nil
}
return followProp.GetIRI()
}
// SetFollowers sets the given IRI on the Following property of 'with'.
func SetFollowing(with WithFollowing, following *url.URL) {
followProp := with.GetActivityStreamsFollowing()
if followProp == nil {
followProp = streams.NewActivityStreamsFollowingProperty()
with.SetActivityStreamsFollowing(followProp)
}
followProp.SetIRI(following)
}
// GetFollowers returns the IRI contained in the Followers property of 'with'.
func GetFollowers(with WithFollowers) *url.URL {
followProp := with.GetActivityStreamsFollowers()
if followProp == nil || !followProp.IsIRI() {
return nil
}
return followProp.GetIRI()
}
// SetFollowers sets the given IRI on the Followers property of 'with'.
func SetFollowers(with WithFollowers, followers *url.URL) {
followProp := with.GetActivityStreamsFollowers()
if followProp == nil {
followProp = streams.NewActivityStreamsFollowersProperty()
with.SetActivityStreamsFollowers(followProp)
}
followProp.SetIRI(followers)
}
// GetFeatured returns the IRI contained in the Featured property of 'with'.
func GetFeatured(with WithFeatured) *url.URL {
featuredProp := with.GetTootFeatured()
if featuredProp == nil || !featuredProp.IsIRI() {
return nil
}
return featuredProp.GetIRI()
}
// SetFeatured sets the given IRI on the Featured property of 'with'.
func SetFeatured(with WithFeatured, featured *url.URL) {
featuredProp := with.GetTootFeatured()
if featuredProp == nil {
featuredProp = streams.NewTootFeaturedProperty()
with.SetTootFeatured(featuredProp)
}
featuredProp.SetIRI(featured)
}
// GetPublished returns the time contained in the Published property of 'with'.
func GetPublished(with WithPublished) time.Time {
publishProp := with.GetActivityStreamsPublished()
if publishProp == nil {
if publishProp == nil || !publishProp.IsXMLSchemaDateTime() {
return time.Time{}
}
return publishProp.Get()
@ -215,7 +356,7 @@ func SetPublished(with WithPublished, published time.Time) {
// GetEndTime returns the time contained in the EndTime property of 'with'.
func GetEndTime(with WithEndTime) time.Time {
endTimeProp := with.GetActivityStreamsEndTime()
if endTimeProp == nil {
if endTimeProp == nil || !endTimeProp.IsXMLSchemaDateTime() {
return time.Time{}
}
return endTimeProp.Get()
@ -240,7 +381,8 @@ func GetClosed(with WithClosed) []time.Time {
closed := make([]time.Time, 0, closedProp.Len())
for i := 0; i < closedProp.Len(); i++ {
at := closedProp.At(i)
if t := at.GetXMLSchemaDateTime(); !t.IsZero() {
if at.IsXMLSchemaDateTime() {
t := at.GetXMLSchemaDateTime()
closed = append(closed, t)
}
}
@ -265,7 +407,7 @@ func AppendClosed(with WithClosed, closed ...time.Time) {
// GetVotersCount returns the integer contained in the VotersCount property of 'with', if found.
func GetVotersCount(with WithVotersCount) int {
votersProp := with.GetTootVotersCount()
if votersProp == nil {
if votersProp == nil || !votersProp.IsXMLSchemaNonNegativeInteger() {
return 0
}
return votersProp.Get()
@ -281,6 +423,25 @@ func SetVotersCount(with WithVotersCount, count int) {
votersProp.Set(count)
}
// GetDiscoverable returns the boolean contained in the Discoverable property of 'with'.
func GetDiscoverable(with WithDiscoverable) bool {
discoverProp := with.GetTootDiscoverable()
if discoverProp == nil || !discoverProp.IsXMLSchemaBoolean() {
return false
}
return discoverProp.Get()
}
// SetDiscoverable sets the given boolean on the Discoverable property of 'with'.
func SetDiscoverable(with WithDiscoverable, discoverable bool) {
discoverProp := with.GetTootDiscoverable()
if discoverProp == nil {
discoverProp = streams.NewTootDiscoverableProperty()
with.SetTootDiscoverable(discoverProp)
}
discoverProp.Set(discoverable)
}
func getIRIs[T TypeOrIRI](prop Property[T]) []*url.URL {
if prop == nil || prop.Len() == 0 {
return nil

View File

@ -42,7 +42,7 @@ func (suite *ResolveTestSuite) TestResolveDocumentAsAccountable() {
b := []byte(suite.typeToJson(suite.document1))
accountable, err := ap.ResolveAccountable(context.Background(), b)
suite.True(gtserror.WrongType(err))
suite.True(gtserror.IsWrongType(err))
suite.EqualError(err, "ResolveAccountable: cannot resolve vocab type *typedocument.ActivityStreamsDocument as accountable")
suite.Nil(accountable)
}

View File

@ -491,6 +491,46 @@ func (suite *InboxPostTestSuite) TestPostEmptyCreate() {
)
}
func (suite *InboxPostTestSuite) TestPostCreateMalformedBlock() {
var (
blockingAcc = suite.testAccounts["remote_account_1"]
blockedAcc = suite.testAccounts["local_account_1"]
activityID = blockingAcc.URI + "/some-new-activity/01FG9C441MCTW3R2W117V2PQK3"
)
block := streams.NewActivityStreamsBlock()
// set the actor property to the block-ing account's URI
actorProp := streams.NewActivityStreamsActorProperty()
actorIRI := testrig.URLMustParse(blockingAcc.URI)
actorProp.AppendIRI(actorIRI)
block.SetActivityStreamsActor(actorProp)
// set the ID property to the blocks's URI
idProp := streams.NewJSONLDIdProperty()
idProp.Set(testrig.URLMustParse(activityID))
block.SetJSONLDId(idProp)
// set the object property with MISSING block-ed URI.
objectProp := streams.NewActivityStreamsObjectProperty()
block.SetActivityStreamsObject(objectProp)
// set the TO property to the target account's IRI
toProp := streams.NewActivityStreamsToProperty()
toIRI := testrig.URLMustParse(blockedAcc.URI)
toProp.AppendIRI(toIRI)
block.SetActivityStreamsTo(toProp)
suite.inboxPost(
block,
blockingAcc,
blockedAcc,
http.StatusBadRequest,
`{"error":"Bad Request: malformed incoming activity"}`,
suite.signatureCheck,
)
}
func (suite *InboxPostTestSuite) TestPostFromBlockedAccount() {
var (
requestingAccount = suite.testAccounts["remote_account_1"]

View File

@ -34,7 +34,7 @@ var SentinelError = errors.New("BUG: error should not be returned") //nolint:rev
// ignoreErrors is an error matching function used to signal which errors
// the result caches should NOT hold onto. these amount to anything non-permanent.
func ignoreErrors(err error) bool {
return !errorsv2.Comparable(
return !errorsv2.IsV2(
err,
// the only cacheable errs,

View File

@ -43,7 +43,7 @@ func (s *sender) sendTemplate(template string, subject string, data any, toAddre
}
if err := smtp.SendMail(s.hostAddress, s.auth, s.from, toAddresses, msg); err != nil {
return gtserror.SetType(err, gtserror.TypeSMTP)
return gtserror.SetSMTP(err)
}
return nil

View File

@ -175,7 +175,7 @@ func (suite *AccountTestSuite) TestDereferenceLocalAccountWithUnknownUsername()
"thisaccountdoesnotexist",
config.GetHost(),
)
suite.True(gtserror.Unretrievable(err))
suite.True(gtserror.IsUnretrievable(err))
suite.EqualError(err, db.ErrNoEntries.Error())
suite.Nil(fetchedAccount)
}
@ -189,7 +189,7 @@ func (suite *AccountTestSuite) TestDereferenceLocalAccountWithUnknownUsernameDom
"thisaccountdoesnotexist",
"localhost:8080",
)
suite.True(gtserror.Unretrievable(err))
suite.True(gtserror.IsUnretrievable(err))
suite.EqualError(err, db.ErrNoEntries.Error())
suite.Nil(fetchedAccount)
}
@ -202,7 +202,7 @@ func (suite *AccountTestSuite) TestDereferenceLocalAccountWithUnknownUserURI() {
fetchingAccount.Username,
testrig.URLMustParse("http://localhost:8080/users/thisaccountdoesnotexist"),
)
suite.True(gtserror.Unretrievable(err))
suite.True(gtserror.IsUnretrievable(err))
suite.EqualError(err, db.ErrNoEntries.Error())
suite.Nil(fetchedAccount)
}

View File

@ -229,7 +229,7 @@ func (d *Dereferencer) DereferenceStatusAncestors(ctx context.Context, username
l.Warnf("orphaned status: http error dereferencing parent: %v)", err)
return nil
case gtserror.Unretrievable(err):
case gtserror.IsUnretrievable(err):
// Not retrievable for some other reason, so just
// bail for now; we can try again later if necessary.
l.Warnf("orphaned status: parent unretrievable: %v)", err)
@ -354,7 +354,7 @@ stackLoop:
// - any http type error for a new status returns unretrievable
_, statusable, _, err := d.getStatusByURI(ctx, username, itemIRI)
if err != nil {
if !gtserror.Unretrievable(err) {
if !gtserror.IsUnretrievable(err) {
l.Errorf("error dereferencing remote status %s: %v", itemIRI, err)
}
continue itemLoop

View File

@ -200,13 +200,18 @@ func (f *federatingActor) PostInboxScheme(ctx context.Context, w http.ResponseWr
//
// Post the activity to the Actor's inbox and trigger side effects .
if err := f.sideEffectActor.PostInbox(ctx, inboxID, activity); err != nil {
// Special case: We know it is a bad request if the object or
// target properties needed to be populated, but weren't.
// Special case: We know it is a bad request if the object or target
// props needed to be populated, or we failed parsing activity details.
// Send the rejection to the peer.
if errors.Is(err, pub.ErrObjectRequired) || errors.Is(err, pub.ErrTargetRequired) {
// Log the original error but return something a bit more generic.
log.Warnf(ctx, "malformed incoming activity: %v", err)
const text = "malformed activity: missing Object and / or Target"
if errors.Is(err, pub.ErrObjectRequired) ||
errors.Is(err, pub.ErrTargetRequired) ||
gtserror.IsMalformed(err) {
// Log malformed activities to help debug.
l = l.WithField("activity", activity)
l.Warnf("malformed incoming activity: %v", err)
const text = "malformed incoming activity"
return false, gtserror.NewErrorBadRequest(errors.New(text), text)
}
@ -234,7 +239,7 @@ func (f *federatingActor) PostInboxScheme(ctx context.Context, w http.ResponseWr
// This check may be removed when the `Exists()` func
// is updated, and/or federating callbacks are handled
// properly.
if !errorsv2.Comparable(
if !errorsv2.IsV2(
err,
db.ErrAlreadyExists,
db.ErrNoEntries,

View File

@ -113,7 +113,7 @@ func (f *federatingDB) NewID(ctx context.Context, t vocab.Type) (idURL *url.URL,
// If an actor URI has been set, create a new ID
// based on actor (i.e. followER not the followEE).
if uri := ap.GetActor(follow); len(uri) == 1 {
if uri := ap.GetActorIRIs(follow); len(uri) == 1 {
if actorAccount, err := f.state.DB.GetAccountByURI(ctx, uri[0].String()); err == nil {
newID, err := id.NewRandomULID()
if err != nil {

View File

@ -35,39 +35,36 @@ const (
errorTypeKey
unrtrvableKey
wrongTypeKey
// Types returnable from Type(...).
TypeSMTP ErrorType = "smtp" // smtp (mail)
smtpKey
malformedKey
)
// Unretrievable checks error for a stored "unretrievable" flag.
//
// Unretrievable indicates that a call to retrieve a resource
// IsUnretrievable indicates that a call to retrieve a resource
// (account, status, attachment, etc) could not be fulfilled,
// either because it was not found locally, or because some
// prerequisite remote resource call failed, making it impossible
// to return the item.
func Unretrievable(err error) bool {
func IsUnretrievable(err error) bool {
_, ok := errors.Value(err, unrtrvableKey).(struct{})
return ok
}
// SetUnretrievable will wrap the given error to store an "unretrievable"
// flag, returning wrapped error. See "Unretrievable" for example use-cases.
// flag, returning wrapped error. See Unretrievable() for example use-cases.
func SetUnretrievable(err error) error {
return errors.WithValue(err, unrtrvableKey, struct{}{})
}
// WrongType checks error for a stored "wrong type" flag. Wrong type
// IsWrongType checks error for a stored "wrong type" flag. Wrong type
// indicates that an ActivityPub URI returned a type we weren't expecting:
// Statusable instead of Accountable, or vice versa, for example.
func WrongType(err error) bool {
func IsWrongType(err error) bool {
_, ok := errors.Value(err, wrongTypeKey).(struct{})
return ok
}
// SetWrongType will wrap the given error to store a "wrong type" flag,
// returning wrapped error. See "WrongType" for example use-cases.
// returning wrapped error. See IsWrongType() for example use-cases.
func SetWrongType(err error) error {
return errors.WithValue(err, wrongTypeKey, struct{}{})
}
@ -86,29 +83,41 @@ func WithStatusCode(err error, code int) error {
return errors.WithValue(err, statusCodeKey, code)
}
// NotFound checks error for a stored "not found" flag. For example
// an error from an outgoing HTTP request due to DNS lookup.
func NotFound(err error) bool {
// IsNotFound checks error for a stored "not found" flag. For
// example an error from an outgoing HTTP request due to DNS lookup.
func IsNotFound(err error) bool {
_, ok := errors.Value(err, notFoundKey).(struct{})
return ok
}
// SetNotFound will wrap the given error to store a "not found" flag,
// returning wrapped error. See NotFound() for example use-cases.
// returning wrapped error. See IsNotFound() for example use-cases.
func SetNotFound(err error) error {
return errors.WithValue(err, notFoundKey, struct{}{})
}
// Type checks error for a stored "type" value. For example
// an error from sending an email may set a value of "smtp"
// to indicate this was an SMTP error.
func Type(err error) ErrorType {
s, _ := errors.Value(err, errorTypeKey).(ErrorType)
return s
// IsSMTP checks error for a stored "smtp" flag. For
// example an error from outgoing SMTP email attempt.
func IsSMTP(err error) bool {
_, ok := errors.Value(err, smtpKey).(struct{})
return ok
}
// SetType will wrap the given error to store a "type" value,
// returning wrapped error. See Type() for example use-cases.
func SetType(err error, errType ErrorType) error {
return errors.WithValue(err, errorTypeKey, errType)
// SetSMTP will wrap the given error to store an "smtp" flag,
// returning wrapped error. See IsSMTP() for example use-cases.
func SetSMTP(err error) error {
return errors.WithValue(err, smtpKey, struct{}{})
}
// IsMalformed checks error for a stored "malformed" flag. For
// example an error from an incoming ActivityStreams type conversion.
func IsMalformed(err error) bool {
_, ok := errors.Value(err, malformedKey).(struct{})
return ok
}
// SetMalformed will wrap the given error to store a "malformed" flag,
// returning wrapped error. See IsMalformed() for example use-cases.
func SetMalformed(err error) error {
return errors.WithValue(err, malformedKey, struct{}{})
}

View File

@ -61,7 +61,7 @@ func newfAt(calldepth int, msgf string, args ...any) error {
}
}
// caller fetches the calling function name, skipping 'depth'. Results are cached per PC.
// caller fetches the calling function name, skipping 'depth'.
func caller(depth int) string {
var pcs [1]uintptr

View File

@ -39,12 +39,15 @@ type WithCode interface {
// Unwrap returns the original error.
// This should *NEVER* be returned to a client as it may contain sensitive information.
Unwrap() error
// Error serializes the original internal error for debugging within the GoToSocial logs.
// This should *NEVER* be returned to a client as it may contain sensitive information.
Error() string
// Safe returns the API-safe version of the error for serialization towards a client.
// There's not much point logging this internally because it won't contain much helpful information.
Safe() string
// Code returns the status code for serving to a client.
Code() int
}

View File

@ -294,7 +294,7 @@ func (c *Client) DoSigned(r *http.Request, sign SignFunc) (rsp *http.Response, e
_ = rsp.Body.Close()
rsp = nil
} else if errorsv2.Comparable(err,
} else if errorsv2.IsV2(err,
context.DeadlineExceeded,
context.Canceled,
ErrBodyTooLarge,

View File

@ -22,7 +22,7 @@ import (
"strings"
)
// Caller fetches the calling function name, skipping 'depth'. Results are cached per PC.
// Caller fetches the calling function name, skipping 'depth'.
func Caller(depth int) string {
var pcs [1]uintptr

29
internal/log/format.go Normal file
View File

@ -0,0 +1,29 @@
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package log
import "codeberg.org/gruf/go-kv/format"
// VarDump returns a serialized, useful log / error output of given variable.
func VarDump(a any) string {
buf := getBuf()
format.Appendf(buf, "{:v}", a)
s := string(buf.B)
putBuf(buf)
return s
}

View File

@ -95,7 +95,7 @@ func (p *ProcessingEmoji) load(ctx context.Context) (*gtsmodel.Emoji, bool, erro
defer func() {
// This is only done when ctx NOT cancelled.
done = err == nil || !errors.Comparable(err,
done = err == nil || !errors.IsV2(err,
context.Canceled,
context.DeadlineExceeded,
)

View File

@ -115,7 +115,7 @@ func (p *ProcessingMedia) load(ctx context.Context) (*gtsmodel.MediaAttachment,
defer func() {
// This is only done when ctx NOT cancelled.
done = err == nil || !errorsv2.Comparable(err,
done = err == nil || !errorsv2.IsV2(err,
context.Canceled,
context.DeadlineExceeded,
)

View File

@ -20,6 +20,7 @@ package middleware
import (
"fmt"
"net/http"
"runtime"
"time"
"codeberg.org/gruf/go-bytesize"
@ -56,7 +57,10 @@ func Logger(logClientIP bool) gin.HandlerFunc {
_ = c.Error(err)
// Dump a stacktrace to error log
callers := errors.GetCallers(3, 10)
pcs := make([]uintptr, 10)
n := runtime.Callers(3, pcs)
iter := runtime.CallersFrames(pcs[:n])
callers := errors.Callers(gatherFrames(iter, n))
log.WithContext(c.Request.Context()).
WithField("stacktrace", callers).Error(err)
}
@ -80,8 +84,9 @@ func Logger(logClientIP bool) gin.HandlerFunc {
}
// Create log entry with fields
l := log.WithContext(c.Request.Context()).
WithFields(fields...)
l := log.New()
l = l.WithContext(c.Request.Context())
l = l.WithFields(fields...)
// Default is info
lvl := level.INFO
@ -119,3 +124,19 @@ func Logger(logClientIP bool) gin.HandlerFunc {
c.Next()
}
}
// gatherFrames gathers runtime frames from a frame iterator.
func gatherFrames(iter *runtime.Frames, n int) []runtime.Frame {
if iter == nil {
return nil
}
frames := make([]runtime.Frame, 0, n)
for {
f, ok := iter.Next()
if !ok {
break
}
frames = append(frames, f)
}
return frames
}

View File

@ -47,7 +47,7 @@ func (p *Processor) EmailTest(ctx context.Context, account *gtsmodel.Account, to
}
if err := p.emailSender.SendTestEmail(toAddress, testData); err != nil {
if errorType := gtserror.Type(err); errorType == gtserror.TypeSMTP {
if gtserror.IsSMTP(err) {
// An error occurred during the SMTP part.
// We should indicate this to the caller, as
// it will likely help them debug the issue.

View File

@ -382,7 +382,7 @@ func (p *Processor) accountsByNamestring(
if err != nil {
// Check for semi-expected error types.
// On one of these, we can continue.
if !gtserror.Unretrievable(err) && !gtserror.WrongType(err) {
if !gtserror.IsUnretrievable(err) && !gtserror.IsWrongType(err) {
err = gtserror.Newf("error looking up @%s@%s as account: %w", username, domain, err)
return gtserror.NewErrorInternalError(err)
}
@ -491,7 +491,7 @@ func (p *Processor) byURI(
if err != nil {
// Check for semi-expected error types.
// On one of these, we can continue.
if !gtserror.Unretrievable(err) && !gtserror.WrongType(err) {
if !gtserror.IsUnretrievable(err) && !gtserror.IsWrongType(err) {
err = gtserror.Newf("error looking up %s as account: %w", uri, err)
return gtserror.NewErrorInternalError(err)
}
@ -509,7 +509,7 @@ func (p *Processor) byURI(
if err != nil {
// Check for semi-expected error types.
// On one of these, we can continue.
if !gtserror.Unretrievable(err) && !gtserror.WrongType(err) {
if !gtserror.IsUnretrievable(err) && !gtserror.IsWrongType(err) {
err = gtserror.Newf("error looking up %s as status: %w", uri, err)
return gtserror.NewErrorInternalError(err)
}

View File

@ -92,7 +92,7 @@ func (p *Processor) Lookup(
false, // never resolve!
)
if err != nil {
if gtserror.Unretrievable(err) {
if gtserror.IsUnretrievable(err) {
// ErrNotRetrievable is fine, just wrap it in
// a 404 to indicate we couldn't find anything.
err := fmt.Errorf("%s not found", query)

File diff suppressed because it is too large Load Diff

View File

@ -363,7 +363,7 @@ func (suite *ASToInternalTestSuite) TestParseFlag3() {
report, err := suite.typeconverter.ASFlagToReport(context.Background(), asFlag)
suite.Nil(report)
suite.EqualError(err, "ASFlagToReport: account with uri http://localhost:8080/users/mr_e_man could not be found in the db")
suite.EqualError(err, "ASFlagToReport: error getting target account http://localhost:8080/users/mr_e_man from database: sql: no rows in result set")
}
func (suite *ASToInternalTestSuite) TestParseFlag4() {
@ -388,7 +388,7 @@ func (suite *ASToInternalTestSuite) TestParseFlag4() {
report, err := suite.typeconverter.ASFlagToReport(context.Background(), asFlag)
suite.Nil(report)
suite.EqualError(err, "ASFlagToReport: flaggable objects contained no recognizable target account uri")
suite.EqualError(err, "ASFlagToReport: missing target account uri for http://fossbros-anonymous.io/db22128d-884e-4358-9935-6a7c3940535d")
}
func (suite *ASToInternalTestSuite) TestParseFlag5() {

View File

@ -1713,7 +1713,7 @@ func (c *Converter) PollVoteToASCreate(
ap.MustSet(ap.SetJSONLDIdStr, ap.WithJSONLDId(create), id)
// Set Create actor appropriately.
ap.AppendActor(create, authorIRI)
ap.AppendActorIRIs(create, authorIRI)
// Set publish time for activity.
ap.SetPublished(create, vote.CreatedAt)

View File

@ -19,7 +19,6 @@ package typeutils
import (
"context"
"errors"
"fmt"
"net/url"
"path"
@ -27,7 +26,6 @@ import (
"strconv"
"strings"
"github.com/superseriousbusiness/gotosocial/internal/ap"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@ -94,22 +92,6 @@ func misskeyReportInlineURLs(content string) []*url.URL {
return urls
}
// getURI is a shortcut/util function for extracting
// the JSONLDId URI of an Activity or Object.
func getURI(withID ap.WithJSONLDId) (*url.URL, string, error) {
idProp := withID.GetJSONLDId()
if idProp == nil {
return nil, "", errors.New("id prop was nil")
}
if !idProp.IsIRI() {
return nil, "", errors.New("id prop was not an IRI")
}
id := idProp.Get()
return id, id.String(), nil
}
// placeholdUnknownAttachments separates any attachments with type `unknown`
// out of the given slice, and returns an `<aside>` tag containing links to
// those attachments, as well as the slice of remaining "known" attachments.

View File

@ -103,7 +103,7 @@ func wrapStatusableInActivity(activity ap.Activityable, status ap.Statusable, ir
appendStatusableToActivity(activity, status, iriOnly)
ap.AppendTo(activity, ap.GetTo(status)...)
ap.AppendCc(activity, ap.GetCc(status)...)
ap.AppendActor(activity, ap.GetAttributedTo(status)...)
ap.AppendActorIRIs(activity, ap.GetAttributedTo(status)...)
ap.SetPublished(activity, ap.GetPublished(status))
}

View File

@ -2,8 +2,6 @@ package result
import (
"context"
"fmt"
"os"
"reflect"
_ "unsafe"
@ -11,8 +9,6 @@ import (
"codeberg.org/gruf/go-errors/v2"
)
var ErrUnsupportedZero = errors.New("")
// Lookup represents a struct object lookup method in the cache.
type Lookup struct {
// Name is a period ('.') separated string
@ -58,7 +54,8 @@ func New[T any](lookups []Lookup, copy func(T) T, cap int) *Cache[T] {
}
// Allocate new cache object
c := &Cache[T]{copy: copy}
c := new(Cache[T])
c.copy = copy // use copy fn.
c.lookups = make([]structKey, len(lookups))
for i, lookup := range lookups {
@ -96,7 +93,7 @@ func (c *Cache[T]) SetEvictionCallback(hook func(T)) {
}
// Free result and call hook.
v := getResultValue[T](res)
v := res.Value.(T)
putResult(res)
hook(v)
})
@ -125,7 +122,7 @@ func (c *Cache[T]) SetInvalidateCallback(hook func(T)) {
}
// Free result and call hook.
v := getResultValue[T](res)
v := res.Value.(T)
putResult(res)
hook(v)
})
@ -135,11 +132,8 @@ func (c *Cache[T]) SetInvalidateCallback(hook func(T)) {
func (c *Cache[T]) IgnoreErrors(ignore func(error) bool) {
if ignore == nil {
ignore = func(err error) bool {
return errors.Comparable(
err,
context.Canceled,
context.DeadlineExceeded,
)
return errors.Is(err, context.Canceled) ||
errors.Is(err, context.DeadlineExceeded)
}
}
c.cache.Lock()
@ -149,99 +143,16 @@ func (c *Cache[T]) IgnoreErrors(ignore func(error) bool) {
// Load will attempt to load an existing result from the cacche for the given lookup and key parts, else calling the provided load function and caching the result.
func (c *Cache[T]) Load(lookup string, load func() (T, error), keyParts ...any) (T, error) {
var zero T
var res *result
info := c.lookups.get(lookup)
key := info.genKey(keyParts)
return c.load(info, key, load)
}
// Get lookup key info by name.
keyInfo := c.lookups.get(lookup)
if !keyInfo.unique {
panic("non-unique lookup does not support load: " + lookup)
}
// Generate cache key string.
ckey := keyInfo.genKey(keyParts)
// Acquire cache lock
c.cache.Lock()
// Look for primary key for cache key (only accept len=1)
if pkeys := keyInfo.pkeys[ckey]; len(pkeys) == 1 {
// Fetch the result for primary key
entry, ok := c.cache.Cache.Get(pkeys[0])
if ok {
// Since the invalidation / eviction hooks acquire a mutex
// lock separately, and only at this point are the pkeys
// updated, there is a chance that a primary key may return
// no matching entry. Hence we have to check for it here.
res = entry.Value.(*result)
}
}
// Done with lock
c.cache.Unlock()
if res == nil {
// Generate fresh result.
value, err := load()
if err != nil {
if c.ignore(err) {
// don't cache this error type
return zero, err
}
// Alloc result.
res = getResult()
// Store error result.
res.Error = err
// This load returned an error, only
// store this item under provided key.
res.Keys = []cacheKey{{
info: keyInfo,
key: ckey,
}}
} else {
// Alloc result.
res = getResult()
// Store value result.
res.Value = value
// This was a successful load, generate keys.
res.Keys = c.lookups.generate(res.Value)
}
var evict func()
// Lock cache.
c.cache.Lock()
defer func() {
// Unlock cache.
c.cache.Unlock()
if evict != nil {
// Call evict.
evict()
}
}()
// Store result in cache.
evict = c.store(res)
}
// Catch and return cached error
if err := res.Error; err != nil {
return zero, err
}
// Copy value from cached result.
v := c.copy(getResultValue[T](res))
return v, nil
// Has checks the cache for a positive result under the given lookup and key parts.
func (c *Cache[T]) Has(lookup string, keyParts ...any) bool {
info := c.lookups.get(lookup)
key := info.genKey(keyParts)
return c.has(info, key)
}
// Store will call the given store function, and on success store the value in the cache as a positive result.
@ -281,24 +192,120 @@ func (c *Cache[T]) Store(value T, store func() error) error {
return nil
}
// Has checks the cache for a positive result under the given lookup and key parts.
func (c *Cache[T]) Has(lookup string, keyParts ...any) bool {
var res *result
// Invalidate will invalidate any result from the cache found under given lookup and key parts.
func (c *Cache[T]) Invalidate(lookup string, keyParts ...any) {
info := c.lookups.get(lookup)
key := info.genKey(keyParts)
c.invalidate(info, key)
}
// Get lookup key info by name.
keyInfo := c.lookups.get(lookup)
if !keyInfo.unique {
panic("non-unique lookup does not support has: " + lookup)
// Clear empties the cache, calling the invalidate callback where necessary.
func (c *Cache[T]) Clear() { c.Trim(100) }
// Trim ensures the cache stays within percentage of total capacity, truncating where necessary.
func (c *Cache[T]) Trim(perc float64) { c.cache.Trim(perc) }
func (c *Cache[T]) load(lookup *structKey, key string, load func() (T, error)) (T, error) {
if !lookup.unique { // ensure this lookup only returns 1 result
panic("non-unique lookup does not support load: " + lookup.name)
}
// Generate cache key string.
ckey := keyInfo.genKey(keyParts)
var (
zero T
res *result
)
// Acquire cache lock
c.cache.Lock()
// Look for primary key for cache key (only accept len=1)
if pkeys := keyInfo.pkeys[ckey]; len(pkeys) == 1 {
if pkeys := lookup.pkeys[key]; len(pkeys) == 1 {
// Fetch the result for primary key
entry, ok := c.cache.Cache.Get(pkeys[0])
if ok {
// Since the invalidation / eviction hooks acquire a mutex
// lock separately, and only at this point are the pkeys
// updated, there is a chance that a primary key may return
// no matching entry. Hence we have to check for it here.
res = entry.Value.(*result)
}
}
// Done with lock
c.cache.Unlock()
if res == nil {
// Generate fresh result.
value, err := load()
if err != nil {
if c.ignore(err) {
// don't cache this error type
return zero, err
}
// Alloc result.
res = getResult()
// Store error result.
res.Error = err
// This load returned an error, only
// store this item under provided key.
res.Keys = []cacheKey{{
info: lookup,
key: key,
}}
} else {
// Alloc result.
res = getResult()
// Store value result.
res.Value = value
// This was a successful load, generate keys.
res.Keys = c.lookups.generate(res.Value)
}
var evict func()
// Lock cache.
c.cache.Lock()
defer func() {
// Unlock cache.
c.cache.Unlock()
if evict != nil {
// Call evict.
evict()
}
}()
// Store result in cache.
evict = c.store(res)
}
// Catch and return cached error
if err := res.Error; err != nil {
return zero, err
}
// Copy value from cached result.
v := c.copy(res.Value.(T))
return v, nil
}
func (c *Cache[T]) has(lookup *structKey, key string) bool {
var res *result
// Acquire cache lock
c.cache.Lock()
// Look for primary key for cache key (only accept len=1)
if pkeys := lookup.pkeys[key]; len(pkeys) == 1 {
// Fetch the result for primary key
entry, ok := c.cache.Cache.Get(pkeys[0])
@ -320,31 +327,6 @@ func (c *Cache[T]) Has(lookup string, keyParts ...any) bool {
return ok
}
// Invalidate will invalidate any result from the cache found under given lookup and key parts.
func (c *Cache[T]) Invalidate(lookup string, keyParts ...any) {
// Get lookup key info by name.
keyInfo := c.lookups.get(lookup)
// Generate cache key string.
ckey := keyInfo.genKey(keyParts)
// Look for primary key for cache key
c.cache.Lock()
pkeys := keyInfo.pkeys[ckey]
delete(keyInfo.pkeys, ckey)
c.cache.Unlock()
// Invalidate all primary keys.
c.cache.InvalidateAll(pkeys...)
}
// Clear empties the cache, calling the invalidate callback where necessary.
func (c *Cache[T]) Clear() { c.Trim(100) }
// Trim ensures the cache stays within percentage of total capacity, truncating where necessary.
func (c *Cache[T]) Trim(perc float64) { c.cache.Trim(perc) }
// store will cache this result under all of its required cache keys.
func (c *Cache[T]) store(res *result) (evict func()) {
var toEvict []*result
@ -425,6 +407,17 @@ func (c *Cache[T]) store(res *result) (evict func()) {
}
}
func (c *Cache[T]) invalidate(lookup *structKey, key string) {
// Look for primary key for cache key
c.cache.Lock()
pkeys := lookup.pkeys[key]
delete(lookup.pkeys, key)
c.cache.Unlock()
// Invalidate all primary keys.
c.cache.InvalidateAll(pkeys...)
}
type result struct {
// Result primary key
PKey int64
@ -438,12 +431,3 @@ type result struct {
// cached error
Error error
}
// getResultValue is a safe way of casting and fetching result value.
func getResultValue[T any](res *result) T {
v, ok := res.Value.(T)
if !ok {
fmt.Fprintf(os.Stderr, "!! BUG: unexpected value type in result: %T\n", res.Value)
}
return v
}

View File

@ -43,9 +43,8 @@ func (sk structKeys) generate(a any) []cacheKey {
v = v.Elem()
}
// Acquire byte buffer
// Acquire buffer
buf := getBuf()
defer putBuf(buf)
outer:
for i := range sk {
@ -80,6 +79,9 @@ outer:
})
}
// Release buf
putBuf(buf)
return keys
}
@ -124,11 +126,12 @@ type structKey struct {
unique bool
// fields is a slice of runtime struct field
// indices, of the fields encompassed by this key.
// indices, of fields encompassed by this key.
fields []structField
// pkeys is a lookup of stored struct key values
// to the primary cache lookup key (int64).
// to the primary cache lookup key (int64). this
// is protected by the main cache mutex.
pkeys map[string][]int64
}
@ -192,9 +195,8 @@ func (sk *structKey) genKey(parts []any) string {
panic(fmt.Sprintf("incorrect no. key parts provided: want=%d received=%d", len(parts), len(sk.fields)))
}
// Acquire byte buffer
// Acquire buffer
buf := getBuf()
defer putBuf(buf)
buf.Reset()
for i, part := range parts {
@ -210,8 +212,13 @@ func (sk *structKey) genKey(parts []any) string {
// Drop last '.'
buf.Truncate(1)
// Return string copy
return string(buf.B)
// Create str copy
str := string(buf.B)
// Release buf
putBuf(buf)
return str
}
type structField struct {

View File

@ -1,5 +1,8 @@
# go-errors
simple but powerful errors library that allows easy wrapping and stacktracing of errors.
to disable stacktraces set the `notrace` build tag.
powerful errors library with a simple API that allows:
- accessing all the standard library errors functions
- wrapping of errors
- adding values to errors, in a similar manner to contexts
- including calling function prefix when tag=errcaller is set
- including stacktrace with error when tag=errtrace is set

26
vendor/codeberg.org/gruf/go-errors/v2/build_caller.go generated vendored Normal file
View File

@ -0,0 +1,26 @@
//go:build errcaller
// +build errcaller
package errors
import (
_ "unsafe"
)
// IncludesCaller is a compile-time flag used to indicate whether
// to include calling function prefix on error wrap / creation.
const IncludesCaller = true
type caller string
// set will set the actual caller value
// only when correct build flag is set.
func (c *caller) set(v string) {
*c = caller(v)
}
// value returns the actual caller value
// only when correct build flag is set
func (c caller) value() string {
return string(c)
}

View File

@ -0,0 +1,18 @@
//go:build !errcaller
// +build !errcaller
package errors
// IncludesCaller is a compile-time flag used to indicate whether
// to include calling function prefix on error wrap / creation.
const IncludesCaller = false
type caller struct{}
// set will set the actual caller value
// only when correct build flag is set.
func (caller) set(string) {}
// value returns the actual caller value
// only when correct build flag is set.
func (caller) value() string { return "" }

20
vendor/codeberg.org/gruf/go-errors/v2/build_notrace.go generated vendored Normal file
View File

@ -0,0 +1,20 @@
//go:build !errtrace
// +build !errtrace
package errors
import "runtime"
// IncludesStacktrace is a compile-time flag used to indicate
// whether to include stacktraces on error wrap / creation.
const IncludesStacktrace = false
type trace struct{}
// set will set the actual trace value
// only when correct build flag is set.
func (trace) set([]runtime.Frame) {}
// value returns the actual trace value
// only when correct build flag is set.
func (trace) value() Callers { return nil }

27
vendor/codeberg.org/gruf/go-errors/v2/build_trace.go generated vendored Normal file
View File

@ -0,0 +1,27 @@
//go:build errtrace
// +build errtrace
package errors
import (
"runtime"
_ "unsafe"
)
// IncludesStacktrace is a compile-time flag used to indicate
// whether to include stacktraces on error wrap / creation.
const IncludesStacktrace = true
type trace []runtime.Frame
// set will set the actual trace value
// only when correct build flag is set.
func (t *trace) set(v []runtime.Frame) {
*t = trace(v)
}
// value returns the actual trace value
// only when correct build flag is set.
func (t trace) value() Callers {
return Callers(t)
}

View File

@ -1,98 +0,0 @@
package errors
import (
"encoding/json"
"runtime"
"strconv"
"strings"
"unsafe"
)
// Callers is a stacktrace of caller PCs.
type Callers []uintptr
// GetCallers returns a Callers slice of PCs, of at most 'depth'.
func GetCallers(skip int, depth int) Callers {
rpc := make([]uintptr, depth)
n := runtime.Callers(skip+1, rpc)
return Callers(rpc[0:n])
}
// Frames fetches runtime frames for a slice of caller PCs.
func (f Callers) Frames() []runtime.Frame {
// Allocate expected frames slice
frames := make([]runtime.Frame, 0, len(f))
// Get frames iterator for PCs
iter := runtime.CallersFrames(f)
for {
// Get next frame in iter
frame, ok := iter.Next()
if !ok {
break
}
// Append to frames slice
frames = append(frames, frame)
}
return frames
}
// MarshalJSON implements json.Marshaler to provide an easy, simple default.
func (f Callers) MarshalJSON() ([]byte, error) {
// JSON-able frame type
type jsonFrame struct {
Func string `json:"func"`
File string `json:"file"`
Line int `json:"line"`
}
// Convert to frames
frames := f.Frames()
// Allocate expected size jsonFrame slice
jsonFrames := make([]jsonFrame, 0, len(f))
for i := 0; i < len(frames); i++ {
frame := frames[i]
// Convert each to jsonFrame object
jsonFrames = append(jsonFrames, jsonFrame{
Func: funcname(frame.Function),
File: frame.File,
Line: frame.Line,
})
}
// marshal converted frames
return json.Marshal(frames)
}
// String will return a simple string representation of receiving Callers slice.
func (f Callers) String() string {
// Guess-timate to reduce allocs
buf := make([]byte, 0, 64*len(f))
// Convert to frames
frames := f.Frames()
for i := 0; i < len(frames); i++ {
frame := frames[i]
// Append formatted caller info
fn := funcname(frame.Function)
buf = append(buf, fn+"()\n\t"+frame.File+":"...)
buf = strconv.AppendInt(buf, int64(frame.Line), 10)
buf = append(buf, '\n')
}
return *(*string)(unsafe.Pointer(&buf))
}
// funcname splits a function name with pkg from its path prefix.
func funcname(name string) string {
i := strings.LastIndex(name, "/")
return name[i+1:]
}

View File

@ -1,35 +0,0 @@
//go:build !notrace
// +build !notrace
package errors
type errormsg struct {
msg string
wrap error
stack Callers
}
func create(msg string, wrap error) *errormsg {
return &errormsg{
msg: msg,
wrap: wrap,
stack: GetCallers(2, 10),
}
}
func (err *errormsg) Error() string {
return err.msg
}
func (err *errormsg) Is(target error) bool {
other, ok := target.(*errormsg)
return ok && (err.msg == other.msg)
}
func (err *errormsg) Unwrap() error {
return err.wrap
}
func (err *errormsg) Stacktrace() Callers {
return err.stack
}

View File

@ -1,33 +0,0 @@
//go:build notrace
// +build notrace
package errors
type errormsg struct {
msg string
wrap error
}
func create(msg string, wrap error) *errormsg {
return &errormsg{
msg: msg,
wrap: wrap,
}
}
func (err *errormsg) Error() string {
return err.msg
}
func (err *errormsg) Is(target error) bool {
other, ok := target.(*errormsg)
return ok && (err.msg == other.msg)
}
func (err *errormsg) Unwrap() error {
return err.wrap
}
func (err *errormsg) Stacktrace() Callers {
return nil
}

View File

@ -1,37 +1,248 @@
package errors
import (
"errors"
"fmt"
"runtime"
)
// New returns a new error created from message.
//
// Note this function cannot be inlined, to ensure expected
// and consistent behaviour in setting trace / caller info.
//
//go:noinline
func New(msg string) error {
return create(msg, nil)
var c caller
var t trace
if IncludesCaller {
pcs := make([]uintptr, 1)
_ = runtime.Callers(2, pcs)
fn := runtime.FuncForPC(pcs[0])
c.set(funcName(fn))
}
if IncludesStacktrace {
pcs := make([]uintptr, 10)
n := runtime.Callers(2, pcs)
iter := runtime.CallersFrames(pcs[:n])
t.set(gatherFrames(iter, n))
}
return &_errormsg{
cfn: c,
msg: msg,
trc: t,
}
}
// Newf returns a new error created from message format and args.
//
// Note this function cannot be inlined, to ensure expected
// and consistent behaviour in setting trace / caller info.
//
//go:noinline
func Newf(msgf string, args ...interface{}) error {
return create(fmt.Sprintf(msgf, args...), nil)
var c caller
var t trace
if IncludesCaller {
pcs := make([]uintptr, 1)
_ = runtime.Callers(2, pcs)
fn := runtime.FuncForPC(pcs[0])
c.set(funcName(fn))
}
if IncludesStacktrace {
pcs := make([]uintptr, 10)
n := runtime.Callers(2, pcs)
iter := runtime.CallersFrames(pcs[:n])
t.set(gatherFrames(iter, n))
}
return &_errormsg{
cfn: c,
msg: fmt.Sprintf(msgf, args...),
trc: t,
}
}
// NewAt returns a new error created, skipping 'skip'
// frames for trace / caller information, from message.
//
// Note this function cannot be inlined, to ensure expected
// and consistent behaviour in setting trace / caller info.
//
//go:noinline
func NewAt(skip int, msg string) error {
var c caller
var t trace
if IncludesCaller {
pcs := make([]uintptr, 1)
_ = runtime.Callers(skip+1, pcs)
fn := runtime.FuncForPC(pcs[0])
c.set(funcName(fn))
}
if IncludesStacktrace {
pcs := make([]uintptr, 10)
n := runtime.Callers(skip+1, pcs)
iter := runtime.CallersFrames(pcs[:n])
t.set(gatherFrames(iter, n))
}
return &_errormsg{
cfn: c,
msg: msg,
trc: t,
}
}
// Wrap will wrap supplied error within a new error created from message.
//
// Note this function cannot be inlined, to ensure expected
// and consistent behaviour in setting trace / caller info.
//
//go:noinline
func Wrap(err error, msg string) error {
return create(msg, err)
if err == nil {
panic("cannot wrap nil error")
}
var c caller
var t trace
if IncludesCaller {
pcs := make([]uintptr, 1)
_ = runtime.Callers(2, pcs)
fn := runtime.FuncForPC(pcs[0])
c.set(funcName(fn))
}
if IncludesStacktrace {
pcs := make([]uintptr, 10)
n := runtime.Callers(2, pcs)
iter := runtime.CallersFrames(pcs[:n])
t.set(gatherFrames(iter, n))
}
return &_errorwrap{
cfn: c,
msg: msg,
err: err,
trc: t,
}
}
// Wrapf will wrap supplied error within a new error created from message format and args.
//
// Note this function cannot be inlined, to ensure expected
// and consistent behaviour in setting trace / caller info.
//
//go:noinline
func Wrapf(err error, msgf string, args ...interface{}) error {
return create(fmt.Sprintf(msgf, args...), err)
if err == nil {
panic("cannot wrap nil error")
}
var c caller
var t trace
if IncludesCaller {
pcs := make([]uintptr, 1)
_ = runtime.Callers(2, pcs)
fn := runtime.FuncForPC(pcs[0])
c.set(funcName(fn))
}
if IncludesStacktrace {
pcs := make([]uintptr, 10)
n := runtime.Callers(2, pcs)
iter := runtime.CallersFrames(pcs[:n])
t.set(gatherFrames(iter, n))
}
return &_errorwrap{
cfn: c,
msg: fmt.Sprintf(msgf, args...),
err: err,
trc: t,
}
}
// WrapAt wraps error within new error created from message,
// skipping 'skip' frames for trace / caller information.
//
// Note this function cannot be inlined, to ensure expected
// and consistent behaviour in setting trace / caller info.
//
//go:noinline
func WrapAt(skip int, err error, msg string) error {
if err == nil {
panic("cannot wrap nil error")
}
var c caller
var t trace
if IncludesCaller {
pcs := make([]uintptr, 1)
_ = runtime.Callers(skip+1, pcs)
fn := runtime.FuncForPC(pcs[0])
c.set(funcName(fn))
}
if IncludesStacktrace {
pcs := make([]uintptr, 10)
n := runtime.Callers(skip+1, pcs)
iter := runtime.CallersFrames(pcs[:n])
t.set(gatherFrames(iter, n))
}
return &_errorwrap{
cfn: c,
msg: msg,
err: err,
trc: t,
}
}
// Stacktrace fetches first stored stacktrace of callers from error chain.
func Stacktrace(err error) Callers {
var e interface {
Stacktrace() Callers
}
if !errors.As(err, &e) {
if !IncludesStacktrace {
// compile-time check
return nil
}
return e.Stacktrace()
if e := AsV2[*_errormsg](err); err != nil {
return e.trc.value()
}
if e := AsV2[*_errorwrap](err); err != nil {
return e.trc.value()
}
return nil
}
type _errormsg struct {
cfn caller
msg string
trc trace
}
func (err *_errormsg) Error() string {
if IncludesCaller {
fn := err.cfn.value()
return fn + " " + err.msg
} else {
return err.msg
}
}
func (err *_errormsg) Is(other error) bool {
oerr, ok := other.(*_errormsg)
return ok && oerr.msg == err.msg
}
type _errorwrap struct {
cfn caller
msg string
err error // wrapped
trc trace
}
func (err *_errorwrap) Error() string {
if IncludesCaller {
fn := err.cfn.value()
return fn + " " + err.msg + ": " + err.err.Error()
} else {
return err.msg + ": " + err.err.Error()
}
}
func (err *_errorwrap) Is(other error) bool {
oerr, ok := other.(*_errorwrap)
return ok && oerr.msg == err.msg
}
func (err *_errorwrap) Unwrap() error {
return err.err
}

View File

@ -2,46 +2,30 @@ package errors
import (
"sync/atomic"
"unsafe"
)
// OnceError is an error structure that supports safe multi
// threaded usage and setting only once (until reset).
type OnceError struct{ err unsafe.Pointer }
// NewOnce returns a new OnceError instance.
func NewOnce() OnceError {
return OnceError{
err: nil,
}
}
type OnceError struct{ ptr atomic.Pointer[error] }
// Store will safely set the OnceError to value, no-op if nil.
func (e *OnceError) Store(err error) {
// Nothing to do
func (e *OnceError) Store(err error) bool {
if err == nil {
return
return false
}
// Only set if not already
atomic.CompareAndSwapPointer(
&e.err,
nil,
unsafe.Pointer(&err),
)
return e.ptr.CompareAndSwap(nil, &err)
}
// Load will load the currently stored error.
func (e *OnceError) Load() error {
return *(*error)(atomic.LoadPointer(&e.err))
if ptr := e.ptr.Load(); ptr != nil {
return *ptr
}
return nil
}
// IsSet returns whether OnceError has been set.
func (e *OnceError) IsSet() bool {
return (atomic.LoadPointer(&e.err) != nil)
}
func (e *OnceError) IsSet() bool { return (e.ptr.Load() != nil) }
// Reset will reset the OnceError value.
func (e *OnceError) Reset() {
atomic.StorePointer(&e.err, nil)
}
func (e *OnceError) Reset() { e.ptr.Store(nil) }

97
vendor/codeberg.org/gruf/go-errors/v2/runtime.go generated vendored Normal file
View File

@ -0,0 +1,97 @@
package errors
import (
"encoding/json"
"runtime"
"strconv"
"strings"
"unsafe"
)
// Callers ...
type Callers []runtime.Frame
// MarshalJSON implements json.Marshaler to provide an easy, simple default.
func (c Callers) MarshalJSON() ([]byte, error) {
// JSON-able frame type
type jsonFrame struct {
Func string `json:"func"`
File string `json:"file"`
Line int `json:"line"`
}
// Allocate expected size jsonFrame slice
jsonFrames := make([]jsonFrame, len(c))
// Convert each to jsonFrame object
for i := 0; i < len(c); i++ {
frame := c[i]
jsonFrames[i] = jsonFrame{
Func: funcName(frame.Func),
File: frame.File,
Line: frame.Line,
}
}
// marshal converted frames
return json.Marshal(jsonFrames)
}
// String will return a simple string representation of receiving Callers slice.
func (c Callers) String() string {
// Guess-timate to reduce allocs
buf := make([]byte, 0, 64*len(c))
for i := 0; i < len(c); i++ {
frame := c[i]
// Append formatted caller info
fn := funcName(frame.Func)
buf = append(buf, fn+"()\n\t"+frame.File+":"...)
buf = strconv.AppendInt(buf, int64(frame.Line), 10)
buf = append(buf, '\n')
}
return *(*string)(unsafe.Pointer(&buf))
}
// funcName formats a function name to a quickly-readable string.
func funcName(fn *runtime.Func) string {
if fn == nil {
return ""
}
// Get func name
// for formatting.
name := fn.Name()
// Drop all but the package name and function name, no mod path
if idx := strings.LastIndex(name, "/"); idx >= 0 {
name = name[idx+1:]
}
const params = `[...]`
// Drop any generic type parameter markers
if idx := strings.Index(name, params); idx >= 0 {
name = name[:idx] + name[idx+len(params):]
}
return name
}
// gatherFrames collates runtime frames from a frame iterator.
func gatherFrames(iter *runtime.Frames, n int) Callers {
if iter == nil {
return nil
}
frames := make([]runtime.Frame, 0, n)
for {
f, ok := iter.Next()
if !ok {
break
}
frames = append(frames, f)
}
return frames
}

View File

@ -1,133 +1,37 @@
package errors
import (
"errors"
"reflect"
_ "unsafe"
"codeberg.org/gruf/go-bitutil"
)
// errtype is a ptr to the error interface type.
var errtype = reflect.TypeOf((*error)(nil)).Elem()
// Is reports whether any error in err's tree matches target.
//
// The tree consists of err itself, followed by the errors obtained by repeatedly
// calling Unwrap. When err wraps multiple errors, Is examines err followed by a
// depth-first traversal of its children.
//
// An error is considered to match a target if it is equal to that target or if
// it implements a method Is(error) bool such that Is(target) returns true.
//
// An error type might provide an Is method so it can be treated as equivalent
// to an existing error. For example, if MyError defines
//
// func (m MyError) Is(target error) bool { return target == fs.ErrExist }
//
// then Is(MyError{}, fs.ErrExist) returns true. See [syscall.Errno.Is] for
// an example in the standard library. An Is method should only shallowly
// compare err and the target and not call Unwrap on either.
//
//go:linkname Is errors.Is
func Is(err error, target error) bool
// Comparable is functionally equivalent to calling errors.Is() on multiple errors (up to a max of 64).
func Comparable(err error, targets ...error) bool {
var flags bitutil.Flags64
// Flags only has 64 bit-slots
if len(targets) > 64 {
panic("too many targets")
}
for i := 0; i < len(targets); {
if targets[i] == nil {
if err == nil {
return true
}
// Drop nil targets from slice.
copy(targets[i:], targets[i+1:])
targets = targets[:len(targets)-1]
continue
}
// Check if this error is directly comparable
if reflect.TypeOf(targets[i]).Comparable() {
flags = flags.Set(uint8(i))
}
i++
}
for err != nil {
// Check if this layer supports .Is interface
is, ok := err.(interface{ Is(error) bool })
if !ok {
// Error does not support interface
//
// Only try perform direct compare
for i := 0; i < len(targets); i++ {
// Try directly compare errors
if flags.Get(uint8(i)) &&
err == targets[i] {
// IsV2 calls Is(err, target) for each target within targets.
func IsV2(err error, targets ...error) bool {
for _, target := range targets {
if Is(err, target) {
return true
}
}
} else {
// Error supports the .Is interface
//
// Perform direct compare AND .Is()
for i := 0; i < len(targets); i++ {
if (flags.Get(uint8(i)) &&
err == targets[i]) ||
is.Is(targets[i]) {
return true
}
}
}
// Unwrap to next layer
err = errors.Unwrap(err)
}
return false
}
// Assignable is functionally equivalent to calling errors.As() on multiple errors,
// except that it only checks assignability as opposed to setting the target.
func Assignable(err error, targets ...error) bool {
if err == nil {
// Fastest case.
return false
}
for i := 0; i < len(targets); {
if targets[i] == nil {
// Drop nil targets from slice.
copy(targets[i:], targets[i+1:])
targets = targets[:len(targets)-1]
continue
}
i++
}
for err != nil {
// Check if this layer supports .As interface
as, ok := err.(interface{ As(any) bool })
// Get reflected err type.
te := reflect.TypeOf(err)
if !ok {
// Error does not support interface.
//
// Check assignability using reflection.
for i := 0; i < len(targets); i++ {
tt := reflect.TypeOf(targets[i])
if te.AssignableTo(tt) {
return true
}
}
} else {
// Error supports the .As interface.
//
// Check using .As() and reflection.
for i := 0; i < len(targets); i++ {
if as.As(targets[i]) {
return true
} else if tt := reflect.TypeOf(targets[i]); // nocollapse
te.AssignableTo(tt) {
return true
}
}
}
// Unwrap to next layer.
err = errors.Unwrap(err)
}
return false
}
@ -152,8 +56,79 @@ func Assignable(err error, targets ...error) bool {
//go:linkname As errors.As
func As(err error, target any) bool
// AsV2 is functionally similar to As(), instead
// leveraging generics to handle allocation and
// returning of a concrete generic parameter type.
func AsV2[Type any](err error) Type {
var t Type
var ok bool
errs := []error{err}
for len(errs) > 0 {
// Pop next error to check.
err := errs[len(errs)-1]
errs = errs[:len(errs)-1]
// Check direct type.
t, ok = err.(Type)
if ok {
return t
}
// Look for .As() support.
as, ok := err.(interface {
As(target any) bool
})
if ok {
// Attempt .As().
if as.As(&t) {
return t
}
}
// Try unwrap errors.
switch u := err.(type) {
case interface{ Unwrap() error }:
errs = append(errs, u.Unwrap())
case interface{ Unwrap() []error }:
errs = append(errs, u.Unwrap()...)
}
}
return t
}
// Unwrap returns the result of calling the Unwrap method on err, if err's
// type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.
// type contains an Unwrap method returning error.
// Otherwise, Unwrap returns nil.
//
// Unwrap only calls a method of the form "Unwrap() error".
// In particular Unwrap does not unwrap errors returned by [Join].
//
//go:linkname Unwrap errors.Unwrap
func Unwrap(err error) error
// UnwrapV2 is functionally similar to Unwrap(), except that
// it also handles the case of interface{ Unwrap() []error }.
func UnwrapV2(err error) []error {
switch u := err.(type) {
case interface{ Unwrap() error }:
if e := u.Unwrap(); err != nil {
return []error{e}
}
case interface{ Unwrap() []error }:
return u.Unwrap()
}
return nil
}
// Join returns an error that wraps the given errors.
// Any nil error values are discarded.
// Join returns nil if every value in errs is nil.
// The error formats as the concatenation of the strings obtained
// by calling the Error method of each element of errs, with a newline
// between each string.
//
// A non-nil error returned by Join implements the Unwrap() []error method.
//
//go:linkname Join errors.Join
func Join(errs ...error) error

View File

@ -1,56 +1,50 @@
package errors
import "errors"
// WithValue wraps err to store given key-value pair, accessible via Value() function.
func WithValue(err error, key any, value any) error {
if err == nil {
panic("nil error")
}
return &errWithValue{
var kvs []kv
if e := AsV2[*errWithValues](err); e != nil {
kvs = e.kvs
}
return &errWithValues{
err: err,
key: key,
val: value,
kvs: append(kvs, kv{key, value}),
}
}
// Value searches for value stored under given key in error chain.
func Value(err error, key any) any {
var e *errWithValue
if !errors.As(err, &e) {
return nil
}
if e := AsV2[*errWithValues](err); e != nil {
return e.Value(key)
}
return nil
}
type errWithValue struct {
// simple key-value type.
type kv struct{ k, v any }
// errWithValues wraps an error to provide key-value storage.
type errWithValues struct {
err error
key any
val any
kvs []kv
}
func (e *errWithValue) Error() string {
func (e *errWithValues) Error() string {
return e.err.Error()
}
func (e *errWithValue) Is(target error) bool {
return e.err == target
func (e *errWithValues) Unwrap() error {
return e.err
}
func (e *errWithValue) Unwrap() error {
return Unwrap(e.err)
}
func (e *errWithValue) Value(key any) any {
for {
if key == e.key {
return e.val
func (e *errWithValues) Value(key any) any {
for i := range e.kvs {
if e.kvs[i].k == key {
return e.kvs[i].v
}
}
if !errors.As(e.err, &e) {
return nil
}
}
}

View File

@ -221,8 +221,15 @@ func worker_run(ctx context.Context, fns <-chan WorkerFunc) bool {
defer func() {
// Recover and drop any panic
if r := recover(); r != nil {
// Gather calling func frames.
pcs := make([]uintptr, 10)
n := runtime.Callers(3, pcs)
i := runtime.CallersFrames(pcs[:n])
c := gatherFrames(i, n)
const msg = "worker_run: recovered panic: %v\n\n%s\n"
fmt.Fprintf(os.Stderr, msg, r, errors.GetCallers(2, 10))
fmt.Fprintf(os.Stderr, msg, r, c.String())
}
}()
@ -243,8 +250,15 @@ func drain_queue(fns <-chan WorkerFunc) bool {
defer func() {
// Recover and drop any panic
if r := recover(); r != nil {
const msg = "drain_queue: recovered panic: %v\n\n%s\n"
fmt.Fprintf(os.Stderr, msg, r, errors.GetCallers(2, 10))
// Gather calling func frames.
pcs := make([]uintptr, 10)
n := runtime.Callers(3, pcs)
i := runtime.CallersFrames(pcs[:n])
c := gatherFrames(i, n)
const msg = "worker_run: recovered panic: %v\n\n%s\n"
fmt.Fprintf(os.Stderr, msg, r, c.String())
}
}()
@ -260,3 +274,19 @@ func drain_queue(fns <-chan WorkerFunc) bool {
}
}
}
// gatherFrames collates runtime frames from a frame iterator.
func gatherFrames(iter *runtime.Frames, n int) errors.Callers {
if iter == nil {
return nil
}
frames := make([]runtime.Frame, 0, n)
for {
f, ok := iter.Next()
if !ok {
break
}
frames = append(frames, f)
}
return frames
}

6
vendor/modules.txt vendored
View File

@ -13,7 +13,7 @@ codeberg.org/gruf/go-bytesize
# codeberg.org/gruf/go-byteutil v1.2.0
## explicit; go 1.16
codeberg.org/gruf/go-byteutil
# codeberg.org/gruf/go-cache/v3 v3.5.6
# codeberg.org/gruf/go-cache/v3 v3.5.7
## explicit; go 1.19
codeberg.org/gruf/go-cache/v3
codeberg.org/gruf/go-cache/v3/result
@ -22,7 +22,7 @@ codeberg.org/gruf/go-cache/v3/ttl
# codeberg.org/gruf/go-debug v1.3.0
## explicit; go 1.16
codeberg.org/gruf/go-debug
# codeberg.org/gruf/go-errors/v2 v2.2.0
# codeberg.org/gruf/go-errors/v2 v2.3.1
## explicit; go 1.19
codeberg.org/gruf/go-errors/v2
# codeberg.org/gruf/go-fastcopy v1.1.2
@ -50,7 +50,7 @@ codeberg.org/gruf/go-maps
# codeberg.org/gruf/go-mutexes v1.3.1
## explicit; go 1.14
codeberg.org/gruf/go-mutexes
# codeberg.org/gruf/go-runners v1.6.1
# codeberg.org/gruf/go-runners v1.6.2
## explicit; go 1.19
codeberg.org/gruf/go-runners
# codeberg.org/gruf/go-sched v1.2.3