@@ -16,12 +16,26 @@ function makeOpts(url) {
16
16
return opts ;
17
17
}
18
18
19
+ function checkError ( e , resp , expectedStatus , msg ) {
20
+ if ( e ) return e ;
21
+
22
+ if ( resp && resp . statusCode !== expectedStatus ) {
23
+ if ( resp . statusCode === 403 )
24
+ msg = msg || 'session expired, please login again' ;
25
+
26
+ return {
27
+ msg : msg || 'http error' ,
28
+ statusCode : resp . statusCode
29
+ } ;
30
+ }
31
+ }
32
+
19
33
var leetcodeClient = { } ;
20
34
21
35
leetcodeClient . getProblems = function ( cb ) {
22
36
request ( makeOpts ( config . PROBLEMS_URL ) , function ( e , resp , body ) {
37
+ e = checkError ( e , resp , 200 ) ;
23
38
if ( e ) return cb ( e ) ;
24
- if ( resp . statusCode !== 200 ) return cb ( 'HTTP failed:' + resp . statusCode ) ;
25
39
26
40
var problems = JSON . parse ( body ) . stat_status_pairs . map ( function ( p ) {
27
41
return {
@@ -49,8 +63,8 @@ var aceCtrl = {
49
63
50
64
leetcodeClient . getProblem = function ( problem , cb ) {
51
65
request ( problem . link , function ( e , resp , body ) {
66
+ e = checkError ( e , resp , 200 ) ;
52
67
if ( e ) return cb ( e ) ;
53
- if ( resp . statusCode !== 200 ) return cb ( 'HTTP failed:' + resp . statusCode ) ;
54
68
55
69
var $ = cheerio . load ( body ) ;
56
70
var info = $ ( 'div[class="question-info text-info"] ul li strong' ) ;
@@ -74,8 +88,8 @@ leetcodeClient.getProblem = function(problem, cb) {
74
88
75
89
leetcodeClient . login = function ( user , cb ) {
76
90
request ( config . LOGIN_URL , function ( e , resp , body ) {
91
+ e = checkError ( e , resp , 200 ) ;
77
92
if ( e ) return cb ( e ) ;
78
- if ( resp . statusCode !== 200 ) return cb ( 'HTTP failed:' + resp . statusCode ) ;
79
93
80
94
user . loginCSRF = h . getSetCookieValue ( resp , 'csrftoken' ) ;
81
95
@@ -93,8 +107,8 @@ leetcodeClient.login = function(user, cb) {
93
107
}
94
108
} ;
95
109
request . post ( opts , function ( e , resp , body ) {
110
+ e = checkError ( e , resp , 302 , 'invalid password?' ) ;
96
111
if ( e ) return cb ( e ) ;
97
- if ( resp . statusCode !== 302 ) return cb ( 'HTTP failed:' + resp . statusCode ) ;
98
112
99
113
user . sessionCSRF = h . getSetCookieValue ( resp , 'csrftoken' ) ;
100
114
user . sessionId = h . getSetCookieValue ( resp , 'PHPSESSID' ) ;
@@ -112,8 +126,8 @@ function verifyResult(opts, jobs, results, cb) {
112
126
113
127
opts . url = config . VERIFY_URL . replace ( '$id' , jobs [ 0 ] . id ) ;
114
128
request . get ( opts , function ( e , resp , body ) {
129
+ e = checkError ( e , resp , 200 ) ;
115
130
if ( e ) return cb ( e ) ;
116
- if ( resp . statusCode !== 200 ) return cb ( 'HTTP failed:' + resp . statusCode ) ;
117
131
118
132
var result = JSON . parse ( body ) ;
119
133
if ( result . state === 'SUCCESS' ) {
@@ -142,8 +156,8 @@ leetcodeClient.testProblem = function(problem, cb) {
142
156
} ;
143
157
144
158
request . post ( opts , function ( e , resp , body ) {
159
+ e = checkError ( e , resp , 200 ) ;
145
160
if ( e ) return cb ( e ) ;
146
- if ( resp . statusCode !== 200 ) return cb ( 'HTTP failed:' + resp . statusCode ) ;
147
161
148
162
opts . json = false ;
149
163
opts . body = null ;
@@ -172,8 +186,8 @@ leetcodeClient.submitProblem = function(problem, cb) {
172
186
} ;
173
187
174
188
request . post ( opts , function ( e , resp , body ) {
189
+ e = checkError ( e , resp , 200 ) ;
175
190
if ( e ) return cb ( e ) ;
176
- if ( resp . statusCode !== 200 ) return cb ( 'HTTP failed:' + resp . statusCode ) ;
177
191
178
192
opts . json = false ;
179
193
opts . body = null ;
0 commit comments