aboutsummaryrefslogtreecommitdiff
path: root/pkg/api/api0/errors.go
blob: e407be6fc7fda6460d549015cbfb69454bc6590d (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package api0

import (
	"fmt"
)

// ErrorCode represents a known Northstar error code.
type ErrorCode string

// https://github.com/R2Northstar/NorthstarMasterServer/blob/b45ff0ef267712e8bff6cd718bb5dc1afcdec420/shared/errorcodes.js
const (
	ErrorCode_NO_GAMESERVER_RESPONSE     ErrorCode = "NO_GAMESERVER_RESPONSE"     // Couldn't reach game server
	ErrorCode_BAD_GAMESERVER_RESPONSE    ErrorCode = "BAD_GAMESERVER_RESPONSE"    // Game server gave an invalid response
	ErrorCode_UNAUTHORIZED_GAMESERVER    ErrorCode = "UNAUTHORIZED_GAMESERVER"    // Game server is not authorized to make that request
	ErrorCode_UNAUTHORIZED_GAME          ErrorCode = "UNAUTHORIZED_GAME"          // Stryder couldn't confirm that this account owns Titanfall 2
	ErrorCode_UNAUTHORIZED_PWD           ErrorCode = "UNAUTHORIZED_PWD"           // Wrong password
	ErrorCode_STRYDER_RESPONSE           ErrorCode = "STRYDER_RESPONSE"           // Got bad response from stryder
	ErrorCode_STRYDER_PARSE              ErrorCode = "STRYDER_PARSE"              // Couldn't parse stryder response
	ErrorCode_PLAYER_NOT_FOUND           ErrorCode = "PLAYER_NOT_FOUND"           // Couldn't find player account
	ErrorCode_INVALID_MASTERSERVER_TOKEN ErrorCode = "INVALID_MASTERSERVER_TOKEN" // Invalid or expired masterserver token
	ErrorCode_JSON_PARSE_ERROR           ErrorCode = "JSON_PARSE_ERROR"           // Error parsing json response
	ErrorCode_UNSUPPORTED_VERSION        ErrorCode = "UNSUPPORTED_VERSION"        // The version you are using is no longer supported
	ErrorCode_DUPLICATE_SERVER           ErrorCode = "DUPLICATE_SERVER"           // A server with this port already exists for your IP address
)

const (
	ErrorCode_INTERNAL_SERVER_ERROR ErrorCode = "INTERNAL_SERVER_ERROR"
	ErrorCode_BAD_REQUEST           ErrorCode = "BAD_REQUEST"
)

// ErrorObj contains an error code and a message for API responses.
type ErrorObj struct {
	Code    ErrorCode `json:"enum"`
	Message string    `json:"msg"` // note: no omitempty
}

// Obj returns an ErrorObj.
func (n ErrorCode) Obj() ErrorObj {
	return ErrorObj{
		Code: n,
	}
}

// MessageObj is like Message, but returns an ErrorObj.
func (n ErrorCode) MessageObj() ErrorObj {
	return ErrorObj{
		Code:    n,
		Message: n.Message(),
	}
}

// MessageObjf is like Messagef, but returns an ErrorObj.
func (n ErrorCode) MessageObjf(format string, a ...interface{}) ErrorObj {
	return ErrorObj{
		Code:    n,
		Message: n.Messagef(format, a...),
	}
}

// Message returns the default message for error code n.
func (n ErrorCode) Message() string {
	switch n {
	case ErrorCode_NO_GAMESERVER_RESPONSE:
		return "Couldn't reach game server"
	case ErrorCode_BAD_GAMESERVER_RESPONSE:
		return "Game server gave an invalid response"
	case ErrorCode_UNAUTHORIZED_GAMESERVER:
		return "Game server is not authorized to make that request"
	case ErrorCode_UNAUTHORIZED_GAME:
		return "Stryder couldn't confirm that this account owns Titanfall 2"
	case ErrorCode_UNAUTHORIZED_PWD:
		return "Wrong password"
	case ErrorCode_STRYDER_RESPONSE:
		return "Got bad response from stryder"
	case ErrorCode_STRYDER_PARSE:
		return "Couldn't parse stryder response"
	case ErrorCode_PLAYER_NOT_FOUND:
		return "Couldn't find player account"
	case ErrorCode_INVALID_MASTERSERVER_TOKEN:
		return "Invalid or expired masterserver token"
	case ErrorCode_JSON_PARSE_ERROR:
		return "Error parsing json response"
	case ErrorCode_UNSUPPORTED_VERSION:
		return "The version you are using is no longer supported"
	case ErrorCode_DUPLICATE_SERVER:
		return "A server with this port already exists for your IP address"
	case ErrorCode_INTERNAL_SERVER_ERROR:
		return "Internal server error"
	case ErrorCode_BAD_REQUEST:
		return "Bad request"
	default:
		return string(n)
	}
}

// Messagef returns Message() with additional text appended after ": ".
func (n ErrorCode) Messagef(format string, a ...interface{}) string {
	if format == "" {
		return n.Message()
	}
	return n.Message() + ": " + fmt.Sprintf(format, a...)
}