Skip to content

Commit 98e9e8b

Browse files
committed
fix(Git): catch errors from git.push operations
Signed-off-by: Marcel Klehr <[email protected]>
1 parent 5ee8a6c commit 98e9e8b

File tree

5 files changed

+42
-7
lines changed

5 files changed

+42
-7
lines changed

_locales/en/messages.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@
128128
"Error043": {
129129
"message": "E043: Failsafe: The current sync run would increase your bookmarks count by {0}%. Refusing to execute. Disable this failsafe in the profile settings if you want to proceed anyway."
130130
},
131+
"Error044": {
132+
"message": "E044: Git push operation failed: {0}"
133+
},
131134
"LabelWebdavurl": {
132135
"message": "WebDAV URL"
133136
},

src/errors/Error.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,15 @@ export class AdditionFailsafeError extends FloccusError {
368368
this.percent = percent
369369
Object.setPrototypeOf(this, AdditionFailsafeError.prototype)
370370
}
371+
}
372+
373+
export class GitPushError extends FloccusError {
374+
public errorMessage: string
375+
376+
constructor(errorMessage:string) {
377+
super(`E044: Git push operation failed: ${errorMessage}`)
378+
this.code = 44
379+
this.errorMessage = errorMessage
380+
Object.setPrototypeOf(this, AdditionFailsafeError.prototype)
381+
}
371382
}

src/lib/adapters/Git.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import http from 'isomorphic-git/http/web'
99
import FS from '@isomorphic-git/lightning-fs'
1010
import Html from '../serializers/Html'
1111
import {
12-
FileUnreadableError,
12+
FileUnreadableError, GitPushError,
1313
MissingPermissionsError,
1414
ResourceLockedError,
1515
SlashError
@@ -133,7 +133,7 @@ export default class GitAdapter extends CachingAdapter {
133133
await git.renameBranch({ fs: this.fs, dir: this.dir, ref: this.server.branch, oldref: currentBranch })
134134
}
135135
Logger.log('(git) push')
136-
await git.push({
136+
const result = await git.push({
137137
fs: this.fs,
138138
http,
139139
dir: this.dir,
@@ -142,6 +142,9 @@ export default class GitAdapter extends CachingAdapter {
142142
remote: 'origin',
143143
onAuth: () => this.onAuth()
144144
})
145+
if (result.error) {
146+
throw new GitPushError(result.error)
147+
}
145148
} else {
146149
throw e
147150
}
@@ -203,19 +206,23 @@ export default class GitAdapter extends CachingAdapter {
203206
})
204207
try {
205208
Logger.log('(git) push')
206-
await git.push({
209+
const result = await git.push({
207210
fs: this.fs,
208211
http,
209212
dir: this.dir,
210213
remote: 'origin',
211214
force: true,
212215
onAuth: () => this.onAuth()
213216
})
217+
if (result.error) {
218+
throw new GitPushError(result.error)
219+
}
214220
} catch (e) {
215221
if (e.code && e.code === git.Errors.PushRejectedError.code) {
216222
await this.freeLock() // Only clears the locks set in the current adapter instance
217223
throw new ResourceLockedError
218224
}
225+
throw e
219226
}
220227
} else {
221228
Logger.log('No changes to the server version necessary')
@@ -244,7 +251,10 @@ export default class GitAdapter extends CachingAdapter {
244251
Logger.log('(git) tag ' + tag)
245252
await git.tag({ fs: this.fs, dir: this.dir, ref: tag })
246253
Logger.log('(git) push tag ' + tag)
247-
await git.push({ fs: this.fs, http, dir: this.dir, ref: tag, onAuth: () => this.onAuth() })
254+
const result = await git.push({ fs: this.fs, http, dir: this.dir, ref: tag, onAuth: () => this.onAuth() })
255+
if (result.error) {
256+
throw new GitPushError(result.error)
257+
}
248258
this.locked.push(tag)
249259
})()
250260
await this.lockingPromise
@@ -265,6 +275,7 @@ export default class GitAdapter extends CachingAdapter {
265275
try {
266276
for (const tag of this.locked) {
267277
Logger.log('(git) push: delete tag ' + tag)
278+
// Ignoring result.error
268279
await git.push({ fs: this.fs, http, dir: this.dir, ref: tag, delete: true, onAuth: () => this.onAuth() })
269280
}
270281
this.locked = []
@@ -281,6 +292,7 @@ export default class GitAdapter extends CachingAdapter {
281292
const tags = await git.listTags({ fs, dir: this.dir })
282293
const lockTags = tags.filter(tag => tag.startsWith('floccus-lock-'))
283294
for (const tag of lockTags) {
295+
// ignoring result.error
284296
await git.push({ fs, http, dir: this.dir, ref: tag, delete: true, onAuth: () => this.onAuth() })
285297
}
286298
}
@@ -359,7 +371,7 @@ export default class GitAdapter extends CachingAdapter {
359371
if (currentBranch && currentBranch !== this.server.branch) {
360372
await git.renameBranch({ fs, dir: this.dir, ref: this.server.branch, oldref: currentBranch })
361373
}
362-
await git.push({
374+
const result = await git.push({
363375
fs,
364376
http,
365377
dir: this.dir,
@@ -369,6 +381,9 @@ export default class GitAdapter extends CachingAdapter {
369381
force: true,
370382
onAuth: () => this.onAuth()
371383
})
384+
if (result.error) {
385+
throw new GitPushError(result.error)
386+
}
372387
await git.fetch({
373388
http,
374389
fs,

src/lib/browser/BrowserAccount.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Account from '../Account'
66
import {
77
AdditionFailsafeError,
88
CreateBookmarkError,
9-
DeletionFailsafeError, FloccusError,
9+
DeletionFailsafeError, FloccusError, GitPushError,
1010
HttpError,
1111
InconsistentBookmarksExistenceError, LockFileError,
1212
MissingItemOrderError,
@@ -119,6 +119,9 @@ export default class BrowserAccount extends Account {
119119
if (er instanceof UpdateBookmarkError) {
120120
return i18n.getMessage('Error' + String(er.code).padStart(3, '0'), [er.bookmark.inspect()])
121121
}
122+
if (er instanceof GitPushError) {
123+
return i18n.getMessage('Error' + String(er.code).padStart(3, '0'), [er.errorMessage])
124+
}
122125
if (er instanceof FloccusError) {
123126
return i18n.getMessage('Error' + String(er.code).padStart(3, '0'))
124127
}

src/lib/native/NativeAccount.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { IAccountData } from '../interfaces/AccountStorage'
66
import {
77
AdditionFailsafeError,
88
CreateBookmarkError,
9-
DeletionFailsafeError, FloccusError,
9+
DeletionFailsafeError, FloccusError, GitPushError,
1010
HttpError,
1111
InconsistentBookmarksExistenceError, LockFileError,
1212
MissingItemOrderError,
@@ -97,6 +97,9 @@ export default class NativeAccount extends Account {
9797
if (er instanceof UpdateBookmarkError) {
9898
return i18n.getMessage('Error' + String(er.code).padStart(3, '0'), [er.bookmark.inspect()])
9999
}
100+
if (er instanceof GitPushError) {
101+
return i18n.getMessage('Error' + String(er.code).padStart(3, '0'), [er.errorMessage])
102+
}
100103
if (er instanceof FloccusError) {
101104
return i18n.getMessage('Error' + String(er.code).padStart(3, '0'))
102105
}

0 commit comments

Comments
 (0)