-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
60 lines (50 loc) · 2.22 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// aahframework.org/ws source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package ws
import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
"aahframework.org/ahttp.v0"
)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Package methods
//______________________________________________________________________________
// IsDisconnected method is helper to identify error is disconnect related.
// If it is returns true otherwise false.
func IsDisconnected(err error) bool {
switch err {
case ErrConnectionClosed, ErrUseOfClosedConnection:
return true
}
return false
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Package Unexported methods
//______________________________________________________________________________
// WriteHTTPError is to write WebSocket context error.
func writeHTTPError(w http.ResponseWriter, code int, body string) {
w.Header().Set(ahttp.HeaderContentType, ahttp.ContentTypePlainText.String())
w.Header().Set(ahttp.HeaderContentLength, strconv.Itoa(len(body)))
w.WriteHeader(code)
_, _ = w.Write([]byte(body))
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Package Unexported methods
//______________________________________________________________________________
// createError method creates aah WebSocket error.
func createError(err error) error {
if err == nil {
return err
}
msg := err.Error()
if strings.HasPrefix(msg, "ws closed") {
return ErrConnectionClosed
} else if err == io.EOF || strings.HasSuffix(msg, "use of closed network connection") {
return ErrUseOfClosedConnection
}
return fmt.Errorf("aah%s", msg)
}