@@ -34,42 +34,65 @@ export default {
3434 const provider = new IndexeddbPersistence (id , yDoc );
3535
3636 provider .on (" synced" , async (_ : any ) => {
37- const metaData = await meta ;
38- const contentData = yDoc .getText (id ).toJSON ();
39-
40- if (! config .credentials ?.github ?.scope ) {
41- config .credentials .github = await GitHub .access_token (code );
42- Utils .storeConfig (config );
43- }
44-
45- const gist = await GitHub .gistUpload (
46- config .credentials .github ,
47- metaData .meta .title ,
48- metaData .meta .macro ?.comment || " " ,
49- contentData ,
50- metaData .meta .gist_id
51- );
52-
53- if (gist .error == " Bad credentials" ) {
54- config .credentials .github = undefined ;
55- Utils .storeConfig (config );
56-
57- this .message = this .$t (' github.badCredentials' );
58-
59- window .location .reload ();
60- }
61-
62- if (metaData .meta .gist_id != gist .id ) {
63- await database .put (id , {
64- gist_id: gist .id ,
65- gist_url: gist .raw_url ,
66- });
67- }
68-
69- this .message = gist .message ;
70-
71- if (gist .url ) {
72- window .location .href = gist .url ;
37+ try {
38+ const metaData = await meta ;
39+ const contentData = yDoc .getText (id ).toJSON ();
40+
41+ if (! config .credentials ?.github ?.scope ) {
42+ const credentials = await GitHub .access_token (code );
43+
44+ // could not obtain a token (e.g. expired/invalid OAuth code)
45+ if (credentials ?.error ) {
46+ config .credentials .github = undefined ;
47+ Utils .storeConfig (config );
48+
49+ this .message = this .$t (' github.error' ) + " \n " + credentials .message ;
50+ return ;
51+ }
52+
53+ config .credentials .github = credentials ;
54+ Utils .storeConfig (config );
55+ }
56+
57+ const gist = await GitHub .gistUpload (
58+ config .credentials .github ,
59+ metaData .meta .title ,
60+ metaData .meta .macro ?.comment || " " ,
61+ contentData ,
62+ metaData .meta .gist_id
63+ );
64+
65+ if (gist .error == " Bad credentials" ) {
66+ config .credentials .github = undefined ;
67+ Utils .storeConfig (config );
68+
69+ this .message = this .$t (' github.badCredentials' );
70+
71+ window .location .reload ();
72+ return ;
73+ }
74+
75+ // any other upload/network error: show it instead of hanging
76+ if (gist .error ) {
77+ this .message = this .$t (' github.error' ) + " \n " + gist .message ;
78+ return ;
79+ }
80+
81+ if (metaData .meta .gist_id != gist .id ) {
82+ await database .put (id , {
83+ gist_id: gist .id ,
84+ gist_url: gist .raw_url ,
85+ });
86+ }
87+
88+ this .message = gist .message ;
89+
90+ if (gist .url ) {
91+ window .location .href = gist .url ;
92+ }
93+ } catch (e ) {
94+ console .warn (e );
95+ this .message = this .$t (' github.error' ) + " \n " + (e ?.message || e );
7396 }
7497 });
7598 },
0 commit comments