diff --git a/foomo/docs/projects/gotsrpc/playground/03_todos.md b/foomo/docs/projects/gotsrpc/playground/03_todos.md index 26877e8..3161ae1 100644 --- a/foomo/docs/projects/gotsrpc/playground/03_todos.md +++ b/foomo/docs/projects/gotsrpc/playground/03_todos.md @@ -16,3 +16,7 @@ https://github.com/foomo/gotsrpc-playground/blob/main/server/server/todos.go ## Next.js TypeScript client + +```typescript reference title="client/pages/wheel-of-fortune.tsx" +https://github.com/foomo/gotsrpc-playground/blob/main/client/pages/todos.tsx +``` diff --git a/foomo/docs/projects/gotsrpc/playground/04_union-errors.md b/foomo/docs/projects/gotsrpc/playground/04_union-errors.md new file mode 100644 index 0000000..f943e8d --- /dev/null +++ b/foomo/docs/projects/gotsrpc/playground/04_union-errors.md @@ -0,0 +1,22 @@ +# Union Errors + +## Go Service + +Service interface defintion + +```go reference title="server/services/wof/service.go" lines +https://github.com/foomo/gotsrpc-playground/blob/main/server/services/ouch/service.go +``` + +Service implementation + +```go reference title="server/services/helloworld/service.go" +https://github.com/foomo/gotsrpc-playground/blob/main/server/server/ouch.go +``` + + +## Next.js TypeScript client + +```typescript reference title="client/pages/wheel-of-fortune.tsx" +https://github.com/foomo/gotsrpc-playground/blob/main/client/pages/ouch.tsx +``` diff --git a/foomo/docs/projects/gotsrpc/service-interfaces/errors.md b/foomo/docs/projects/gotsrpc/service-interfaces/errors.md index 585f20e..4d8d6bd 100644 --- a/foomo/docs/projects/gotsrpc/service-interfaces/errors.md +++ b/foomo/docs/projects/gotsrpc/service-interfaces/errors.md @@ -1,2 +1,90 @@ # Errors +On the Go server side errors are types, that implement the error type: + +```go +// from https://go.dev/blog/error-handling-and-go +type error interface { + Error() string +} +``` + +This does not transport well, since an interface implementation can not be marshalled. + +## String Error Types + +Scalar string error types provide a nice way to combine Go constants, that translate to TypeScript enums as errors: + +```go title="Go" +type ScalarError string + +const ( + ErrorFoo ScalarError = "foo" + ErrorBar ScalarError = "bar" +) + +func (e *ScalarError) Error() string { + return string(*e) +} + +type Service interface { + MightGoWrong() *ScalarError +} +``` + +```typescript title="TypeScript" +export enum ScalarError { + ErrorBar = "bar", + ErrorFoo = "foo", +} + +export interface ServiceClient { + mightGoWrong(): Promise; +} +``` + +## Struct Error Types + +If an enumeration is not enough and you want to add information to your errors a struct is a good choice (be careful not to expose secrets 😉) : + +- it can still implement the Error type +- it is still typed in contrast to other alternatives like maps + +```go title="Go" +type ErrorCode int + +const ( + ErrorCodeFoo ErrorCode = 1 + ErrorCodeBar ErrorCode = 2 +) + +type StructError struct { + Message string `json:"message,omitempty"` + Code ErrorCode `json:"errorCode"` +} + +func (e *StructError) Error() string { + return e.Message +} + +type Service struct { + MightGoWrong() *StructError +} +``` + +```typescript title="TypeScript" +export enum ErrorCode { + Bar = 2, + Foo = 1, +} + +export interface StructError { + message?:string; + errorCode:ErrorCode; +} + +export interface ServiceClient { + mightGoWrong(): Promise; +} +``` +