github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/code.go (about) 1 // Copyright 2021 Edward McFarlane. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package larking 6 7 import ( 8 "net/http" 9 10 "google.golang.org/grpc/codes" 11 "nhooyr.io/websocket" 12 ) 13 14 var codeToHTTPStatus = [...]int{ 15 http.StatusOK, // 0 16 http.StatusRequestTimeout, // 1 17 http.StatusInternalServerError, // 2 18 http.StatusBadRequest, // 3 19 http.StatusGatewayTimeout, // 4 20 http.StatusNotFound, // 5 21 http.StatusConflict, // 6 22 http.StatusForbidden, // 7 23 http.StatusTooManyRequests, // 8 24 http.StatusBadRequest, // 9 25 http.StatusConflict, // 10 26 http.StatusBadRequest, // 11 27 http.StatusNotImplemented, // 12 28 http.StatusInternalServerError, // 13 29 http.StatusServiceUnavailable, // 14 30 http.StatusInternalServerError, // 15 31 http.StatusUnauthorized, // 16 32 } 33 34 func HTTPStatusCode(c codes.Code) int { 35 if int(c) > len(codeToHTTPStatus) { 36 return http.StatusInternalServerError 37 } 38 return codeToHTTPStatus[c] 39 } 40 41 // TODO: validate error codes. 42 var codeToWSStatus = [...]websocket.StatusCode{ 43 websocket.StatusNormalClosure, // 0 44 websocket.StatusGoingAway, // 1 45 websocket.StatusInternalError, // 2 46 websocket.StatusUnsupportedData, // 3 47 websocket.StatusGoingAway, // 4 48 websocket.StatusInternalError, // 5 49 websocket.StatusInternalError, // 6 50 websocket.StatusInternalError, // 7 51 websocket.StatusInternalError, // 8 52 websocket.StatusInternalError, // 9 53 websocket.StatusInternalError, // 10 54 websocket.StatusInternalError, // 11 55 websocket.StatusUnsupportedData, // 12 56 websocket.StatusInternalError, // 13 57 websocket.StatusInternalError, // 14 58 websocket.StatusInternalError, // 15 59 websocket.StatusPolicyViolation, // 16 60 } 61 62 func WSStatusCode(c codes.Code) websocket.StatusCode { 63 if int(c) > len(codeToHTTPStatus) { 64 return websocket.StatusInternalError 65 } 66 return codeToWSStatus[c] 67 }