45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package probe
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"supply-intelligence/internal/domain"
|
|
)
|
|
|
|
var ErrUnknownStatusCode = errors.New("unknown probe status code")
|
|
|
|
func ClassifyHTTPResult(statusCode int, transportErr error) (domain.ProbeClassification, string, error) {
|
|
if transportErr != nil {
|
|
return domain.ProbeClassificationInconclusive, "transport_error", nil
|
|
}
|
|
|
|
switch statusCode {
|
|
case http.StatusOK:
|
|
return domain.ProbeClassificationSuccess, "ok", nil
|
|
case http.StatusUnauthorized:
|
|
fallthrough
|
|
case http.StatusForbidden:
|
|
return domain.ProbeClassificationExplicitFailure, "auth_rejected", nil
|
|
case http.StatusTooManyRequests:
|
|
fallthrough
|
|
case http.StatusInternalServerError:
|
|
fallthrough
|
|
case http.StatusBadGateway:
|
|
fallthrough
|
|
case http.StatusServiceUnavailable:
|
|
fallthrough
|
|
case http.StatusGatewayTimeout:
|
|
return domain.ProbeClassificationInconclusive, "upstream_unstable", nil
|
|
default:
|
|
if statusCode >= 500 {
|
|
return domain.ProbeClassificationInconclusive, "upstream_unstable", nil
|
|
}
|
|
if statusCode >= 400 {
|
|
return domain.ProbeClassificationInconclusive, "unexpected_client_error", nil
|
|
}
|
|
return "", "", fmt.Errorf("%w: %d", ErrUnknownStatusCode, statusCode)
|
|
}
|
|
}
|