chore: 初始化 Slurm Go SDK 项目骨架和核心客户端
添加 Go 模块定义、.gitignore、包文档、HTTP Client 核心、Token 认证和错误处理。 Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
38
internal/slurm/errors.go
Normal file
38
internal/slurm/errors.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package slurm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// ErrorResponse represents an error returned by the Slurm REST API.
|
||||
type ErrorResponse struct {
|
||||
Response *http.Response
|
||||
Message string
|
||||
}
|
||||
|
||||
func (r *ErrorResponse) Error() string {
|
||||
return fmt.Sprintf("%v %v: %d %s",
|
||||
r.Response.Request.Method, r.Response.Request.URL,
|
||||
r.Response.StatusCode, r.Message)
|
||||
}
|
||||
|
||||
// CheckResponse checks the API response for errors. It returns nil if the
|
||||
// response is a 2xx status code. For non-2xx codes, it reads the response
|
||||
// body and returns an ErrorResponse.
|
||||
func CheckResponse(r *http.Response) error {
|
||||
if c := r.StatusCode; c >= 200 && c <= 299 {
|
||||
return nil
|
||||
}
|
||||
|
||||
errorResponse := &ErrorResponse{Response: r}
|
||||
data, err := io.ReadAll(r.Body)
|
||||
if err != nil || len(data) == 0 {
|
||||
errorResponse.Message = r.Status
|
||||
return errorResponse
|
||||
}
|
||||
|
||||
errorResponse.Message = string(data)
|
||||
return errorResponse
|
||||
}
|
||||
Reference in New Issue
Block a user