-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: error handling and proper status codes (#5)
- Loading branch information
1 parent
b5a0b3f
commit bfbf5e8
Showing
13 changed files
with
1,004 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package backend | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
|
||
"github.com/moeru-ai/unspeech/pkg/utils" | ||
"github.com/samber/lo" | ||
"github.com/samber/mo" | ||
) | ||
|
||
var _ error = (*JSONResponseError)(nil) | ||
|
||
type JSONResponseError struct { | ||
StatusCode int `json:"status_code"` | ||
Message string `json:"message"` | ||
|
||
bodyParsed map[string]any | ||
} | ||
|
||
func NewJSONResponseError(statusCode int, responseBody io.Reader) mo.Result[*JSONResponseError] { | ||
jsonData, err := io.ReadAll(responseBody) | ||
if err != nil { | ||
return mo.Err[*JSONResponseError](err) | ||
} | ||
|
||
resp := &JSONResponseError{ | ||
StatusCode: statusCode, | ||
} | ||
|
||
err = json.Unmarshal(jsonData, &resp.bodyParsed) | ||
if err != nil { | ||
return mo.Err[*JSONResponseError](err) | ||
} | ||
|
||
errorMessage := utils.GetByJSONPath[string](resp.bodyParsed, "{ .message }") | ||
errorStr := utils.GetByJSONPath[string](resp.bodyParsed, "{ .error }") | ||
errorMap := utils.GetByJSONPath[map[string]any](resp.bodyParsed, "{ .error }") | ||
errorStrFromErrorMap := utils.GetByJSONPath[string](errorMap, "{ .message }") | ||
|
||
resp.Message = lo.Must(lo.Coalesce(errorMessage, errorStr, errorStrFromErrorMap, "Unknown error")) | ||
|
||
return mo.Ok(resp) | ||
} | ||
|
||
func (r *JSONResponseError) Error() string { | ||
return r.Message | ||
} | ||
|
||
var _ error = (*TextResponseError)(nil) | ||
|
||
type TextResponseError struct { | ||
StatusCode int `json:"status_code"` | ||
Body string `json:"body"` | ||
} | ||
|
||
func (r *TextResponseError) Error() string { | ||
return r.Body | ||
} | ||
|
||
func NewTextResponseError(statusCode int, responseBody io.Reader) mo.Result[*TextResponseError] { | ||
data, err := io.ReadAll(responseBody) | ||
if err != nil { | ||
return mo.Err[*TextResponseError](err) | ||
} | ||
|
||
return mo.Ok(&TextResponseError{ | ||
StatusCode: statusCode, | ||
Body: string(data), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.