Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* text=auto eol=lf
internal/testdata/gen/** linguist-generated=true
*.pb.go linguist-generated=true
20 changes: 19 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,30 @@ permissions:
pull-requests: read

jobs:
generated-code-up-to-date:
name: Generated Code Up-To-Date
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5.0.0
- uses: bufbuild/buf-setup-action@v1.50.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Regenerate Go bindings
run: buf generate
- name: Verify clean working tree
run: |
if ! git diff --quiet --exit-code; then
echo "Generated code is out of date. Run 'buf generate' and commit the result."
git diff
exit 1
fi

quality-assurance:
name: Quality Assurance
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [1.24.x]
go-version: [1.26.x]

steps:
- uses: actions/checkout@v5.0.0
Expand Down
16 changes: 16 additions & 0 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: v2
managed:
enabled: true
override:
- file_option: go_package_prefix
value: github.com/TrogonStack/trogonerror/internal/testdata/gen
- file_option: go_package_prefix
module: buf.build/trogonstack/trogon-proto
value: github.com/TrogonStack/trogonproto/gen
plugins:
- remote: buf.build/protocolbuffers/go:v1.36.5
out: internal/testdata/gen
opt:
- paths=source_relative
inputs:
- directory: internal/testdata/proto
5 changes: 5 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,8 @@ type ErrorTemplate struct {
message string // empty string means use code's default message
visibility Visibility
help *Help
metadata Metadata
fields []protoFieldSpec
}

// TemplateOption represents options that can be applied to ErrorTemplate
Expand Down Expand Up @@ -866,6 +868,9 @@ func (et *ErrorTemplate) NewError(options ...ErrorOption) *TrogonError {
if et.help != nil {
baseOptions = append(baseOptions, WithHelp(*et.help))
}
for key, value := range et.metadata {
baseOptions = append(baseOptions, WithMetadataValue(value.visibility, key, value.value))
}

return NewError(et.domain, et.reason, append(baseOptions, options...)...)
}
Expand Down
25 changes: 25 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,33 @@ import (
"time"

"github.com/TrogonStack/trogonerror"
testdatav1 "github.com/TrogonStack/trogonerror/internal/testdata/gen/trogonerror/testdata/v1"
)

var userNotFoundTpl = trogonerror.NewErrorTemplateFromProto[*testdatav1.UserNotFound]()

func ExampleNewErrorTemplateFromProto() {
err := userNotFoundTpl.FromProto(&testdatav1.UserNotFound{
UserId: "gid://shopify/Customer/1234567890",
})

fmt.Println(err.Error())
// Output:
// User does not exist.
// visibility: PUBLIC
// domain: shopify.users
// reason: USER_NOT_FOUND
// code: NOT_FOUND
// metadata:
// - component: users visibility=PUBLIC
// - region: us-east-1 visibility=PUBLIC
// - team: platform-identity visibility=INTERNAL
// - tenantId: default-tenant visibility=PUBLIC
// - userId: gid://shopify/Customer/1234567890 visibility=PUBLIC
//
// - User Docs: https://docs.shopify.com/users
}

func ExampleNewError() {
err := trogonerror.NewError("shopify.users", "NOT_FOUND",
trogonerror.WithCode(trogonerror.CodeNotFound),
Expand Down
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
module github.com/TrogonStack/trogonerror

go 1.24.2
go 1.26

require github.com/stretchr/testify v1.11.1
require (
github.com/TrogonStack/trogonproto v0.1.0
github.com/stretchr/testify v1.11.1
google.golang.org/protobuf v1.36.11
)

require (
buf.build/gen/go/elixir-protobuf/protobuf/protocolbuffers/go v1.36.11-20260114033128-d590dff383e9.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
buf.build/gen/go/elixir-protobuf/protobuf/protocolbuffers/go v1.36.11-20260114033128-d590dff383e9.1 h1:YyaDi4kIyhOWTWYN0OzoKC+Wo+DpSC5aQxfe69fVUWA=
buf.build/gen/go/elixir-protobuf/protobuf/protocolbuffers/go v1.36.11-20260114033128-d590dff383e9.1/go.mod h1:fQtoq8UKgFiCFI3I7uyYxA2iVU1CplNN0PJpdFLdXHI=
github.com/TrogonStack/trogonproto v0.1.0 h1:l3Skhw6rY4zskjS2d8GfXK9fKB6n5lIKObksAjknpUg=
github.com/TrogonStack/trogonproto v0.1.0/go.mod h1:RfEvGMmPYqH8RLE1I63nJZ3OmLUbBa4XSjya06aPLVE=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
184 changes: 184 additions & 0 deletions internal/testdata/gen/trogonerror/testdata/v1/errors.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions internal/testdata/proto/buf.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generated by buf. DO NOT EDIT.
version: v2
deps:
- name: buf.build/elixir-protobuf/protobuf
commit: d590dff383e94f28bacec0e8015c4daa
digest: b5:fa4a17975e0ed6b54e3e7637d659a6358fb18ebd8a931462003cea62d43336fb02c8685076e4cb5129875b13f13c2c6dfe7205f2215d7666efdfc5cce836a2de
- name: buf.build/protocolbuffers/wellknowntypes
commit: 4e1ccfa6827947beb55974645a315b8d
digest: b5:eb5228b1abd02064d6ff0248918500c1ec1ce7df69126af3f220c0b67d81ff45bdf9f016a8e66cd9c1e534f18afc6d8e090d400604c5331d551a68d05f7e7be9
- name: buf.build/trogonstack/trogon-proto
commit: e7442b2c978348c9864565ec086db7a5
digest: b5:2dba0ce198c5981e5d870610f5a5eaadf499d28bffb9d34fd4df5decc35ac5cbcedb670eeaf431312ab59e6d4050f7eb5559a5715e3db06322033658269468ef
6 changes: 6 additions & 0 deletions internal/testdata/proto/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: v2
modules:
- path: .
deps:
- buf.build/trogonstack/trogon-proto
- buf.build/elixir-protobuf/protobuf
34 changes: 34 additions & 0 deletions internal/testdata/proto/trogonerror/testdata/v1/errors.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
syntax = "proto3";

package trogonerror.testdata.v1;

import "trogon/error/v1alpha1/code.proto";
import "trogon/error/v1alpha1/options.proto";
import "trogon/error/v1alpha1/visibility.proto";

message UserNotFound {
option (trogon.error.v1alpha1.message).template = {
domain: "shopify.users",
reason: "USER_NOT_FOUND",
message: "User does not exist.",
code: NOT_FOUND,
visibility: VISIBILITY_PUBLIC,
help_links: [{url: "https://docs.shopify.com/users", description: "User Docs"}],
metadata: [
{key: "component", value: "users", visibility: VISIBILITY_PUBLIC},
{key: "team", value: "platform-identity", visibility: VISIBILITY_INTERNAL}
]
};

string user_id = 1 [(trogon.error.v1alpha1.field) = {
visibility: VISIBILITY_PUBLIC
}];
string tenant_id = 2 [(trogon.error.v1alpha1.field) = {
visibility: VISIBILITY_PUBLIC,
default_value: "default-tenant"
}];
string region = 3 [(trogon.error.v1alpha1.field) = {
visibility: VISIBILITY_PUBLIC,
value: "us-east-1"
}];
}
Loading
Loading