66const requestFn = require ( 'request' ) ;
77const { resolve } = require ( 'url' ) ;
88const { promisify } = require ( 'util' ) ;
9- const httpStatus = require ( '../httpStatus' ) ;
10- const errorCode = require ( './errors' ) ;
9+ const httpStatus = require ( './httpStatus' ) ;
10+ const {
11+ RepositoryEmptyError,
12+ GitHubInternalError,
13+ AuthorizationError
14+ } = require ( './errors' ) ;
15+
1116const {
1217 trimSlashes,
1318 decodeToUtf8
14- } = require ( '.. /helpers' ) ;
19+ } = require ( './helpers' ) ;
1520
1621const GITHUB_BLOB_MODE = '100644' ;
1722const GITHUB_BLOB_TYPE = 'blob' ;
18- const ERR_FILE_NOT_FOUND = 'ENOENT' ;
1923
20- exports . constants = { GITHUB_BLOB_MODE , GITHUB_BLOB_TYPE , ERR_FILE_NOT_FOUND } ;
21- exports . errorCode = errorCode ;
24+ exports . constants = { GITHUB_BLOB_MODE , GITHUB_BLOB_TYPE } ;
2225
2326exports . Client = class GithubClient {
2427 /**
@@ -75,6 +78,19 @@ exports.Client = class GithubClient {
7578 return this . userId ;
7679 }
7780
81+ /**
82+ * Generate an error object based on the status code
83+ * @param {number } statusCode
84+ * @returns {Error }
85+ */
86+ static generateError ( statusCode ) {
87+ return ( message ) => {
88+ if ( statusCode === httpStatus . UNAUTHORIZED ) return new AuthorizationError ( message ) ;
89+ if ( statusCode === httpStatus . CONFLICT ) return new RepositoryEmptyError ( message ) ;
90+ return new GitHubInternalError ( message ) ;
91+ } ;
92+ }
93+
7894 /**
7995 * Get github user id
8096 * @returns {void }
@@ -86,8 +102,7 @@ exports.Client = class GithubClient {
86102 } ) ;
87103
88104 if ( status !== httpStatus . OK ) {
89- this . logger . error ( body ) ;
90- throw new Error ( 'Failed to fetch user details' ) ;
105+ throw GithubClient . generateError ( status ) ( `Failed to fetch user details: ${ body . message } ` ) ;
91106 }
92107
93108 const { login } = body ;
@@ -106,15 +121,8 @@ exports.Client = class GithubClient {
106121 path : `/repos/${ this . userId } /${ this . repoConfig . name } /git/refs/${ refs } ` // default notes branch is 'master'
107122 } ) ;
108123
109- if ( status === httpStatus . CONFLICT ) {
110- const error = new Error ( body . message ) ;
111- error . code = errorCode . ERR_REPOSITORY_EMPTY ;
112- throw error ;
113- }
114-
115124 if ( status !== httpStatus . OK ) {
116- this . logger . error ( body ) ;
117- throw new Error ( 'Failed to fetch head' ) ;
125+ throw GithubClient . generateError ( status ) ( `Failed to fetch head: ${ body . message } ` ) ;
118126 }
119127
120128 const { object } = body ;
@@ -133,8 +141,7 @@ exports.Client = class GithubClient {
133141 } ) ;
134142
135143 if ( status !== httpStatus . OK ) {
136- this . logger . error ( body ) ;
137- throw new Error ( 'Failed to fetch commit stats' ) ;
144+ throw GithubClient . generateError ( status ) ( `Failed to fetch commit stats: ${ body . message } ` ) ;
138145 }
139146
140147 const { message, tree } = body ;
@@ -156,8 +163,7 @@ exports.Client = class GithubClient {
156163 body : { content, encoding }
157164 } ) ;
158165 if ( status !== httpStatus . CREATED ) {
159- this . logger . error ( body ) ;
160- throw new Error ( 'Failed to create blob from content' ) ;
166+ throw GithubClient . generateError ( status ) ( `Failed to create blob from content: ${ body . message } ` ) ;
161167 }
162168
163169 const { sha } = body ;
@@ -170,8 +176,7 @@ exports.Client = class GithubClient {
170176 path : `/repos/${ this . userId } /${ this . repoConfig . name } /git/blobs/${ blobSHA } `
171177 } ) ;
172178 if ( status !== httpStatus . OK ) {
173- this . logger . error ( body ) ;
174- throw new Error ( `Failed to get blob ${ blobSHA } ` ) ;
179+ throw GithubClient . generateError ( status ) ( `Failed to get blob ${ blobSHA } : ${ body . message } ` ) ;
175180 }
176181
177182 const { content, encoding } = body ;
@@ -201,8 +206,7 @@ exports.Client = class GithubClient {
201206 }
202207
203208 if ( status !== httpStatus . OK ) {
204- this . logger . error ( body ) ;
205- throw new Error ( `Failed to get tree ${ treeHash } ` ) ;
209+ throw GithubClient . generateError ( status ) ( `Failed to get tree ${ treeHash } : ${ body . message } ` ) ;
206210 }
207211 const { tree, truncated } = body ;
208212 return { tree, truncated } ;
@@ -233,8 +237,7 @@ exports.Client = class GithubClient {
233237 } ) ;
234238
235239 if ( status !== httpStatus . CREATED ) {
236- this . logger . error ( body ) ;
237- throw new Error ( 'Failed to update tree' ) ;
240+ throw GithubClient . generateError ( status ) ( `Failed to update tree: ${ body . message } ` ) ;
238241 }
239242 return body . sha ;
240243 }
@@ -252,8 +255,7 @@ exports.Client = class GithubClient {
252255 } ) ;
253256
254257 if ( status !== httpStatus . CREATED ) {
255- this . logger . error ( body ) ;
256- throw new Error ( 'Failed to update tree' ) ;
258+ throw GithubClient . generateError ( status ) ( `Failed to update tree: ${ body . message } ` ) ;
257259 }
258260 return body . sha ;
259261 }
@@ -283,8 +285,7 @@ exports.Client = class GithubClient {
283285 } ) ;
284286
285287 if ( status !== httpStatus . CREATED ) {
286- this . logger . error ( body ) ;
287- throw new Error ( 'Failed to commit file' ) ;
288+ throw GithubClient . generateError ( status ) ( `Failed to commit file: ${ body . message } ` ) ;
288289 }
289290 return body . sha ;
290291 }
@@ -305,8 +306,7 @@ exports.Client = class GithubClient {
305306 } ) ;
306307
307308 if ( status !== httpStatus . OK ) {
308- this . logger . error ( body ) ;
309- throw new Error ( 'Failed to update head' ) ;
309+ throw GithubClient . generateError ( status ) ( `Failed to update head: ${ body . message } ` ) ;
310310 }
311311 return body ;
312312 }
@@ -330,8 +330,7 @@ exports.Client = class GithubClient {
330330 } ) ;
331331
332332 if ( status !== httpStatus . CREATED ) {
333- this . logger . error ( body . message ) ;
334- throw new Error ( 'Failed to initialize repository' ) ;
333+ throw GithubClient . generateError ( status ) ( `Failed to initialize repository: ${ body . message } ` ) ;
335334 }
336335
337336 const { commit } = body ;
0 commit comments