Right now, the only way to create a chat is through `POST /api/v2/chats`. That's fine for clients, but an integration running inside coderd (say, a Slack bot) would have to store or mint an API key just to call its own server. So, we'll pull chat creation out of the handler into a shared function, and add `api.CreateChatAsUser` for in-process callers. They get the same validation, errors, and audit entry as the endpoint, and the endpoint's behaviour doesn't change. This is the same pattern `chatCreateWorkspace` already uses to let chatd create workspaces as a user. I've also exported the chat daemon for in-process callers. Chat errors are now just values that carry the endpoint's status, message, and cause, so in-process callers can use `errors.As` on them (e.g. to spot a prompt hook denial) rather than decoding an HTTP response. The audit entry is written in the background, as auto-archive already does, rather than through a fake request. As with an API key, a dormant user is activated on use, whilst a suspended or deleted user gets a 403. The PRs above this one do the same for sending, interrupting, and updating chats.
92 lines
1.9 KiB
Go
92 lines
1.9 KiB
Go
package httperror
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
type Responder interface {
|
|
Response() (int, codersdk.Response)
|
|
}
|
|
|
|
func IsResponder(err error) (Responder, bool) {
|
|
var responseErr Responder
|
|
if errors.As(err, &responseErr) {
|
|
return responseErr, true
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
func NewResponseError(status int, resp codersdk.Response) error {
|
|
return &responseError{
|
|
status: status,
|
|
response: resp,
|
|
}
|
|
}
|
|
|
|
// WrapResponseError returns an HTTP response error that unwraps to cause.
|
|
func WrapResponseError(status int, resp codersdk.Response, cause error) error {
|
|
return &responseError{
|
|
status: status,
|
|
response: resp,
|
|
cause: cause,
|
|
}
|
|
}
|
|
|
|
// StatusCode returns the status that WriteResponseError writes for err.
|
|
func StatusCode(err error) int {
|
|
if responseErr, ok := IsResponder(err); ok {
|
|
code, _ := responseErr.Response()
|
|
return code
|
|
}
|
|
return http.StatusInternalServerError
|
|
}
|
|
|
|
func WriteResponseError(ctx context.Context, rw http.ResponseWriter, err error) {
|
|
if responseErr, ok := IsResponder(err); ok {
|
|
code, resp := responseErr.Response()
|
|
|
|
httpapi.Write(ctx, rw, code, resp)
|
|
return
|
|
}
|
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
|
Message: "Internal server error",
|
|
Detail: err.Error(),
|
|
})
|
|
}
|
|
|
|
type responseError struct {
|
|
status int
|
|
response codersdk.Response
|
|
cause error
|
|
}
|
|
|
|
var (
|
|
_ error = (*responseError)(nil)
|
|
_ Responder = (*responseError)(nil)
|
|
)
|
|
|
|
func (e *responseError) Error() string {
|
|
return fmt.Sprintf("%s: %s", e.response.Message, e.response.Detail)
|
|
}
|
|
|
|
func (e *responseError) Unwrap() error {
|
|
return e.cause
|
|
}
|
|
|
|
func (e *responseError) Status() int {
|
|
return e.status
|
|
}
|
|
|
|
func (e *responseError) Response() (int, codersdk.Response) {
|
|
return e.status, e.response
|
|
}
|
|
|
|
var ErrResourceNotFound = NewResponseError(http.StatusNotFound, httpapi.ResourceNotFoundResponse)
|