forked from butorenjin/easyrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyrtc_util.js
143 lines (122 loc) · 4.38 KB
/
easyrtc_util.js
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* Utility functions specific to EasyRTC.
*
* @module easyrtc_util
* @author Priologic Software, [email protected]
* @copyright Copyright 2014 Priologic Software. All rights reserved.
* @license BSD v2, see LICENSE file in module root folder.
*/
var util = require("util");
var _ = require("underscore"); // General utility functions external module
var g = require("./general_util"); // General utility functions local module
var e = require("./easyrtc_private_obj"); // EasyRTC private object
/**
* Object to hold EasyRTC Utility methods and classes.
*
* @class
*/
var eu = module.exports;
/**
* Disconnects socket. Failure results in a debug level log message.
*
* @param {Object} socket Socket.io connection object.
*/
eu.socketDisconnect = function(socket) {
try {
socket.disconnect();
} catch(err) {
eu.log("debug", "Socket disconnection command failed. Socket may already be disconnected.");
}
};
/**
* Custom Error Object for EasyRTC Server Errors.
*
* @param {string} msg Text message describing the error.
*/
eu.ServerError = function(msg) {
eu.ServerError.super_.call(this, msg, this.constructor);
};
util.inherits(eu.ServerError, g.AbstractError);
eu.ServerError.prototype.name = "Server Error";
eu.ServerError.prototype.errorLevel = "error";
/**
* Custom Error Object for EasyRTC Application Errors.
*
* @param {string} msg Text message describing the error.
*/
eu.ApplicationError = function(msg) {
eu.ApplicationError.super_.call(this, msg, this.constructor);
};
util.inherits(eu.ApplicationError, g.AbstractError);
eu.ApplicationError.prototype.name = "Application Error";
eu.ApplicationError.prototype.errorLevel = "error";
/**
* Custom Error Object for Connection Errors.
*
* @param {string} msg Text message describing the error.
*/
eu.ConnectionError = function(msg) {
eu.ConnectionError.super_.call(this, msg, this.constructor);
};
util.inherits(eu.ConnectionError, g.AbstractError);
eu.ConnectionError.prototype.name = "Connection Error";
eu.ConnectionError.prototype.errorLevel = "error";
/**
* Custom Error Object for EasyRTC Server Warnings.
*
* @param {string} msg Text message describing the error.
*/
eu.ServerWarning = function(msg) {
eu.ServerWarning.super_.call(this, msg, this.constructor);
};
util.inherits(eu.ServerWarning, g.AbstractError);
eu.ServerWarning.prototype.name = "Server Warning";
eu.ServerWarning.prototype.errorLevel = "warning";
/**
* Custom Error Object for EasyRTC Application Warnings.
*
* @param {string} msg Text message describing the error.
*/
eu.ApplicationWarning = function(msg) {
eu.ApplicationWarning.super_.call(this, msg, this.constructor);
};
util.inherits(eu.ApplicationWarning, g.AbstractError);
eu.ApplicationWarning.prototype.name = "Application Warning";
eu.ApplicationWarning.prototype.errorLevel = "warning";
/**
* Custom Error Object for Connection Warnings.
*
* @param {string} msg Text message describing the error.
*/
eu.ConnectionWarning = function(msg) {
eu.ConnectionWarning.super_.call(this, msg, this.constructor);
};
util.inherits(eu.ConnectionWarning, g.AbstractError);
eu.ConnectionWarning.prototype.name = "Connection Warning";
eu.ConnectionWarning.prototype.errorLevel = "warning";
/**
* Determines if an Error object is an instance of ApplicationError, ConnectionError, or ServerError. If it is, it will return true.
*
* @param {Error} err
* @return {Boolean}
*/
eu.isError = function(err) {
if (err && ((err instanceof eu.ConnectionError)||(err instanceof eu.ApplicationError)||(err instanceof eu.ServerError)||(err instanceof Error))) {
return true;
} else {
return false;
}
};
/**
* Determines if an Error object is an instance of ApplicationWarning, ConnectionWarning, or ServerWarning. If it is, it will return true.
*
* @param {Error} err
* @return {Boolean}
*/
eu.isWarning = function(err) {
if (err && ((err instanceof eu.ConnectionWarning)||(err instanceof eu.ApplicationWarning)||(err instanceof eu.ServerWarning))) {
return true;
} else {
return false;
}
};