mirror of
https://github.com/foomo/foomo-docs.git
synced 2025-10-16 12:35:40 +00:00
feat: add more gotsrpc error docs
This commit is contained in:
parent
d0dc058b03
commit
162114c675
@ -16,3 +16,7 @@ https://github.com/foomo/gotsrpc-playground/blob/main/server/server/todos.go
|
|||||||
|
|
||||||
|
|
||||||
## Next.js TypeScript client
|
## 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
|
||||||
|
```
|
||||||
|
|||||||
22
foomo/docs/projects/gotsrpc/playground/04_union-errors.md
Normal file
22
foomo/docs/projects/gotsrpc/playground/04_union-errors.md
Normal file
@ -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
|
||||||
|
```
|
||||||
@ -1,2 +1,90 @@
|
|||||||
# Errors
|
# 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<ScalarError | null>;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 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<StructError | null>;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user