@@ -16,12 +16,26 @@ function makeOpts(url) {
1616 return opts ;
1717}
1818
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+
1933var leetcodeClient = { } ;
2034
2135leetcodeClient . getProblems = function ( cb ) {
2236 request ( makeOpts ( config . PROBLEMS_URL ) , function ( e , resp , body ) {
37+ e = checkError ( e , resp , 200 ) ;
2338 if ( e ) return cb ( e ) ;
24- if ( resp . statusCode !== 200 ) return cb ( 'HTTP failed:' + resp . statusCode ) ;
2539
2640 var problems = JSON . parse ( body ) . stat_status_pairs . map ( function ( p ) {
2741 return {
@@ -49,8 +63,8 @@ var aceCtrl = {
4963
5064leetcodeClient . getProblem = function ( problem , cb ) {
5165 request ( problem . link , function ( e , resp , body ) {
66+ e = checkError ( e , resp , 200 ) ;
5267 if ( e ) return cb ( e ) ;
53- if ( resp . statusCode !== 200 ) return cb ( 'HTTP failed:' + resp . statusCode ) ;
5468
5569 var $ = cheerio . load ( body ) ;
5670 var info = $ ( 'div[class="question-info text-info"] ul li strong' ) ;
@@ -74,8 +88,8 @@ leetcodeClient.getProblem = function(problem, cb) {
7488
7589leetcodeClient . login = function ( user , cb ) {
7690 request ( config . LOGIN_URL , function ( e , resp , body ) {
91+ e = checkError ( e , resp , 200 ) ;
7792 if ( e ) return cb ( e ) ;
78- if ( resp . statusCode !== 200 ) return cb ( 'HTTP failed:' + resp . statusCode ) ;
7993
8094 user . loginCSRF = h . getSetCookieValue ( resp , 'csrftoken' ) ;
8195
@@ -93,8 +107,8 @@ leetcodeClient.login = function(user, cb) {
93107 }
94108 } ;
95109 request . post ( opts , function ( e , resp , body ) {
110+ e = checkError ( e , resp , 302 , 'invalid password?' ) ;
96111 if ( e ) return cb ( e ) ;
97- if ( resp . statusCode !== 302 ) return cb ( 'HTTP failed:' + resp . statusCode ) ;
98112
99113 user . sessionCSRF = h . getSetCookieValue ( resp , 'csrftoken' ) ;
100114 user . sessionId = h . getSetCookieValue ( resp , 'PHPSESSID' ) ;
@@ -112,8 +126,8 @@ function verifyResult(opts, jobs, results, cb) {
112126
113127 opts . url = config . VERIFY_URL . replace ( '$id' , jobs [ 0 ] . id ) ;
114128 request . get ( opts , function ( e , resp , body ) {
129+ e = checkError ( e , resp , 200 ) ;
115130 if ( e ) return cb ( e ) ;
116- if ( resp . statusCode !== 200 ) return cb ( 'HTTP failed:' + resp . statusCode ) ;
117131
118132 var result = JSON . parse ( body ) ;
119133 if ( result . state === 'SUCCESS' ) {
@@ -142,8 +156,8 @@ leetcodeClient.testProblem = function(problem, cb) {
142156 } ;
143157
144158 request . post ( opts , function ( e , resp , body ) {
159+ e = checkError ( e , resp , 200 ) ;
145160 if ( e ) return cb ( e ) ;
146- if ( resp . statusCode !== 200 ) return cb ( 'HTTP failed:' + resp . statusCode ) ;
147161
148162 opts . json = false ;
149163 opts . body = null ;
@@ -172,8 +186,8 @@ leetcodeClient.submitProblem = function(problem, cb) {
172186 } ;
173187
174188 request . post ( opts , function ( e , resp , body ) {
189+ e = checkError ( e , resp , 200 ) ;
175190 if ( e ) return cb ( e ) ;
176- if ( resp . statusCode !== 200 ) return cb ( 'HTTP failed:' + resp . statusCode ) ;
177191
178192 opts . json = false ;
179193 opts . body = null ;
0 commit comments