2023-04-04 10:59:41 +10:00
package workspaceapps
import (
2024-01-17 12:06:59 -06:00
"fmt"
2023-04-04 10:59:41 +10:00
"net/http"
"net/url"
2025-12-08 14:16:00 -09:00
"path"
2023-04-04 10:59:41 +10:00
2026-01-08 10:29:41 +04:00
"cdr.dev/slog/v3"
2024-01-17 12:06:59 -06:00
"github.com/coder/coder/v2/codersdk"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/site"
2023-04-04 10:59:41 +10:00
)
// WriteWorkspaceApp404 writes a HTML 404 error page for a workspace app. If
// appReq is not nil, it will be used to log the request details at debug level.
2023-09-19 16:54:51 -05:00
//
// The 'warnings' parameter is sent to the user, 'details' is only shown in the logs.
func WriteWorkspaceApp404 ( log slog . Logger , accessURL * url . URL , rw http . ResponseWriter , r * http . Request , appReq * Request , warnings [] string , details string ) {
2023-04-04 10:59:41 +10:00
if appReq != nil {
slog . Helper ()
log . Debug ( r . Context (),
2023-09-19 16:54:51 -05:00
"workspace app 404: " + details ,
2023-04-04 10:59:41 +10:00
slog . F ( "username_or_id" , appReq . UsernameOrID ),
slog . F ( "workspace_and_agent" , appReq . WorkspaceAndAgent ),
slog . F ( "workspace_name_or_id" , appReq . WorkspaceNameOrID ),
slog . F ( "agent_name_or_id" , appReq . AgentNameOrID ),
slog . F ( "app_slug_or_port" , appReq . AppSlugOrPort ),
2023-10-11 07:02:39 +11:00
slog . F ( "hostname_prefix" , appReq . Prefix ),
2023-09-19 16:54:51 -05:00
slog . F ( "warnings" , warnings ),
2023-04-04 10:59:41 +10:00
)
}
site . RenderStaticErrorPage ( rw , r , site . ErrorPageData {
2025-12-08 14:16:00 -09:00
Status : http . StatusNotFound ,
Title : "Application Not Found" ,
Description : "The application or workspace you are trying to access does not exist or you do not have permission to access it." ,
Warnings : warnings ,
Actions : [] site . Action {
{
URL : accessURL . String (),
Text : "Back to site" ,
},
},
2023-04-04 10:59:41 +10:00
})
}
// WriteWorkspaceApp500 writes a HTML 500 error page for a workspace app. If
// appReq is not nil, it's fields will be added to the logged error message.
func WriteWorkspaceApp500 ( log slog . Logger , accessURL * url . URL , rw http . ResponseWriter , r * http . Request , appReq * Request , err error , msg string ) {
ctx := r . Context ()
if appReq != nil {
slog . Helper ()
ctx = slog . With ( ctx ,
slog . F ( "username_or_id" , appReq . UsernameOrID ),
slog . F ( "workspace_and_agent" , appReq . WorkspaceAndAgent ),
slog . F ( "workspace_name_or_id" , appReq . WorkspaceNameOrID ),
slog . F ( "agent_name_or_id" , appReq . AgentNameOrID ),
slog . F ( "app_name_or_port" , appReq . AppSlugOrPort ),
2023-10-11 07:02:39 +11:00
slog . F ( "hostname_prefix" , appReq . Prefix ),
2023-04-04 10:59:41 +10:00
)
}
log . Warn ( ctx ,
"workspace app auth server error: " + msg ,
slog . Error ( err ),
)
site . RenderStaticErrorPage ( rw , r , site . ErrorPageData {
2025-12-08 14:16:00 -09:00
Status : http . StatusInternalServerError ,
Title : "Internal Server Error" ,
Description : "An internal server error occurred." ,
Actions : [] site . Action {
{
URL : accessURL . String (),
Text : "Back to site" ,
},
},
2023-04-04 10:59:41 +10:00
})
}
2026-03-26 00:57:28 +11:00
// WriteWorkspaceAppOffline writes a HTML 404 error page for a workspace app. If
2023-04-04 10:59:41 +10:00
// appReq is not nil, it will be used to log the request details at debug level.
func WriteWorkspaceAppOffline ( log slog . Logger , accessURL * url . URL , rw http . ResponseWriter , r * http . Request , appReq * Request , msg string ) {
if appReq != nil {
slog . Helper ()
log . Debug ( r . Context (),
"workspace app unavailable: " + msg ,
slog . F ( "username_or_id" , appReq . UsernameOrID ),
slog . F ( "workspace_and_agent" , appReq . WorkspaceAndAgent ),
slog . F ( "workspace_name_or_id" , appReq . WorkspaceNameOrID ),
slog . F ( "agent_name_or_id" , appReq . AgentNameOrID ),
slog . F ( "app_slug_or_port" , appReq . AppSlugOrPort ),
2023-10-11 07:02:39 +11:00
slog . F ( "hostname_prefix" , appReq . Prefix ),
2023-04-04 10:59:41 +10:00
)
}
site . RenderStaticErrorPage ( rw , r , site . ErrorPageData {
2026-03-26 00:57:28 +11:00
Status : http . StatusNotFound ,
2025-12-08 14:16:00 -09:00
Title : "Application Unavailable" ,
Description : msg ,
Actions : [] site . Action {
{
Text : "Retry" ,
},
{
URL : accessURL . String (),
Text : "Back to site" ,
},
},
2023-04-04 10:59:41 +10:00
})
}
2024-01-17 12:06:59 -06:00
// WriteWorkspaceOffline writes a HTML 400 error page for a workspace app. If
// appReq is not nil, it will be used to log the request details at debug level.
func WriteWorkspaceOffline ( log slog . Logger , accessURL * url . URL , rw http . ResponseWriter , r * http . Request , appReq * Request ) {
if appReq != nil {
slog . Helper ()
log . Debug ( r . Context (),
"workspace app unavailable: workspace stopped" ,
slog . F ( "username_or_id" , appReq . UsernameOrID ),
slog . F ( "workspace_and_agent" , appReq . WorkspaceAndAgent ),
slog . F ( "workspace_name_or_id" , appReq . WorkspaceNameOrID ),
slog . F ( "agent_name_or_id" , appReq . AgentNameOrID ),
slog . F ( "app_slug_or_port" , appReq . AppSlugOrPort ),
slog . F ( "hostname_prefix" , appReq . Prefix ),
)
}
2025-12-08 14:16:00 -09:00
actions := [] site . Action {
{
URL : accessURL . String (),
Text : "Back to site" ,
},
}
workspaceURL , err := url . Parse ( accessURL . String ())
if err == nil {
workspaceURL . Path = path . Join ( accessURL . Path , "@" + appReq . UsernameOrID , appReq . WorkspaceNameOrID )
actions = append ( actions , site . Action {
URL : workspaceURL . String (),
Text : "View workspace" ,
})
}
2024-01-17 12:06:59 -06:00
site . RenderStaticErrorPage ( rw , r , site . ErrorPageData {
2025-12-08 14:16:00 -09:00
Status : http . StatusBadRequest ,
Title : "Workspace Offline" ,
Description : fmt . Sprintf ( "Last workspace transition was to the %q state. Start the workspace to access its applications." , codersdk . WorkspaceTransitionStop ),
Actions : actions ,
2024-01-17 12:06:59 -06:00
})
}