@@ -282,7 +282,7 @@ async function checkUpdate(account, silent = false) {
282
282
}
283
283
}
284
284
285
- async function checkLogin ( ) {
285
+ async function checkLogin ( alertExpired = true ) {
286
286
if ( Settings . accessToken ) {
287
287
try {
288
288
ui . busyLogin ( true ) ;
@@ -291,6 +291,9 @@ async function checkLogin() {
291
291
const userContext = await udemyService . fetchProfile ( Settings . accessToken , 30000 ) ;
292
292
293
293
if ( ! userContext . header . isLoggedIn ) {
294
+ if ( alertExpired ) {
295
+ showAlert ( translate ( "Token expired" ) ) ;
296
+ }
294
297
ui . resetToLogin ( ) ;
295
298
return ;
296
299
}
@@ -516,8 +519,8 @@ function resetCourse($course, $elMessage, autoRetry, courseData, subtitle) {
516
519
} else {
517
520
$course . attr ( "course-completed" , "" ) ;
518
521
519
- if ( $elMessage . hasClass ( "download-error" ) ) {
520
- if ( autoRetry && courseData . errorCount ++ < 5 ) {
522
+ if ( $elMessage . hasClass ( "download-error" ) && autoRetry && courseData ) {
523
+ if ( courseData . errorCount ++ < 5 ) {
521
524
$course . length = 1 ;
522
525
startDownload ( $course , courseData , subtitle ) ;
523
526
return ;
@@ -648,7 +651,7 @@ async function fetchCourseContent(courseId, courseName, courseUrl) {
648
651
showAlert ( `Id: ${ courseId } ` , translate ( "Course not found" ) ) ;
649
652
return null ;
650
653
}
651
- // console.log(" fetchCourseContent" , response);
654
+ console . log ( ` fetchCourseContent ( ${ courseId } )` , response ) ;
652
655
653
656
const downloadType = Number ( Settings . download . type ) ;
654
657
const downloadAttachments = downloadType === Settings . DownloadType . Both || downloadType === Settings . DownloadType . OnlyAttachments ;
@@ -672,7 +675,7 @@ async function fetchCourseContent(courseId, courseName, courseUrl) {
672
675
}
673
676
chapterData = { id : item . id , name : item . title . trim ( ) , lectures : [ ] } ;
674
677
}
675
- else if ( type == "quiz" ) {
678
+ else if ( type == "quiz" || type == "practice" ) {
676
679
const srcUrl = `${ courseUrl } t/${ item . _class } /${ item . id } ` ;
677
680
678
681
chapterData . lectures . push ( {
@@ -765,22 +768,7 @@ async function fetchCourseContent(courseId, courseName, courseUrl) {
765
768
// ui.busyBuildingCourseData(false);
766
769
return courseData ;
767
770
} catch ( error ) {
768
- let msgError ;
769
- const statusCode = error . response ? error . response . status : 0 ;
770
- switch ( statusCode ) {
771
- case 403 :
772
- msgError = translate ( "You do not have permission to access this course" ) ;
773
- prompt . alert ( msgError ) ;
774
- break ;
775
- case 504 :
776
- msgError = "Gateway timeout" ;
777
- break ;
778
- default :
779
- msgError = error . message ;
780
- break ;
781
- }
782
- appendLog ( `EBUILDING_COURSE_DATA: ${ error . code } (${ statusCode } )` , msgError ) ;
783
- throw utils . newError ( "EBUILDING_COURSE_DATA" , msgError ) ;
771
+ handleApiError ( error , "EBUILDING_COURSE_DATA" , courseName , true ) ;
784
772
}
785
773
}
786
774
@@ -795,10 +783,7 @@ async function fetchCourses(isSubscriber) {
795
783
}
796
784
} )
797
785
. catch ( e => {
798
- const statusCode = ( e . response ?. status || 0 ) . toString ( ) + ( e . code ? ` :${ e . code } ` : "" ) ;
799
- appendLog ( `EFETCHING_COURSES: ${ e . code } (${ statusCode } )` , e . message ) ;
800
- //showAlert(error.message, "Fetching Courses");
801
- throw utils . newError ( "EFETCHING_COURSES" , e . message ) ;
786
+ handleApiError ( e , "EFETCHING_COURSES" ) ;
802
787
} )
803
788
. finally ( ( ) => {
804
789
ui . busyLoadCourses ( false ) ;
@@ -821,7 +806,7 @@ function loadMore(loadMoreButton) {
821
806
}
822
807
} ) . catch ( e => {
823
808
const statusCode = ( e . response ?. status || 0 ) . toString ( ) + ( e . code ? ` :${ e . code } ` : "" ) ;
824
- appendLog ( `loadMore_Error : (${ statusCode } )` , e ) ;
809
+ appendLog ( `ELOADING_MORE : (${ statusCode } )` , e ) ;
825
810
} ) . finally ( ( ) => {
826
811
ui . busyLoadCourses ( false ) ;
827
812
} ) ;
@@ -834,8 +819,7 @@ async function search(keyword) {
834
819
const courses = await udemyService . fetchSearchCourses ( keyword , PAGE_SIZE , Settings . subscriber ) ;
835
820
renderCourses ( courses , ! ! keyword ) ;
836
821
} catch ( error ) {
837
- const statusCode = ( error . response ?. status || 0 ) . toString ( ) + ( error . code ? ` :${ error . code } ` : "" ) ;
838
- appendLog ( `search_Error: (${ statusCode } )` , error ) ;
822
+ handleApiError ( error , "ESEARCHING_COURSES" , null , false ) ;
839
823
} finally {
840
824
ui . busyLoadCourses ( false ) ;
841
825
}
@@ -984,8 +968,9 @@ async function prepareDownloading($course, subtitle) {
984
968
985
969
console . clear ( ) ;
986
970
971
+ let courseData = null ;
987
972
try {
988
- const courseData = await fetchCourseContent ( courseId , courseName , courseUrl ) ;
973
+ courseData = await fetchCourseContent ( courseId , courseName , courseUrl ) ;
989
974
if ( ! courseData ) {
990
975
ui . showProgress ( $course , false ) ;
991
976
return ;
@@ -1006,22 +991,11 @@ async function prepareDownloading($course, subtitle) {
1006
991
}
1007
992
1008
993
} catch ( error ) {
1009
- let msgError ;
1010
- const statusCode = error . response ?. status || 0 ;
1011
- switch ( statusCode ) {
1012
- case 403 :
1013
- msgError = translate ( "You do not have permission to access this course" ) + `\nId: ${ courseId } ` ;
1014
- showAlert ( msgError , "Download Error" ) ;
1015
- break ;
1016
- case 504 :
1017
- msgError = "Gateway timeout" ;
1018
- break ;
1019
- default :
1020
- msgError = error ;
1021
- }
1022
- const errorCode = error . code ? ` :${ error . code } ` : "" ;
1023
- appendLog ( `EPREPARE_DOWNLOADING: (${ statusCode } ${ errorCode } )` , msgError ) ;
994
+ const errorName = error . name === "EASK_FOR_SUBTITLE" ? error . name : "EPREPARE_DOWNLOADING" ;
995
+ handleApiError ( error , errorName , null , false ) ;
1024
996
ui . busyOff ( ) ;
997
+
998
+ resetCourse ( $course , $course . find ( ".download-error" ) , Settings . download . autoRetry , courseData , subtitle ) ;
1025
999
}
1026
1000
}
1027
1001
@@ -1173,9 +1147,7 @@ function startDownload($course, courseData, subTitle = "") {
1173
1147
fs . mkdirSync ( seqName . fullPath , { recursive : true } ) ;
1174
1148
downloadLecture ( chapterIndex , lectureIndex , countLectures , seqName . name ) ;
1175
1149
} catch ( error ) {
1176
- appendLog ( "downloadChapter_Error:" , error ) ;
1177
- dialog . showErrorBox ( "downloadChapter_Error" , error . message ) ;
1178
-
1150
+ handleApiError ( error , "EDOWNLOADING_CHAPTER" , null , false ) ;
1179
1151
resetCourse ( $course , $course . find ( ".download-error" ) , false , courseData ) ;
1180
1152
}
1181
1153
}
@@ -1266,9 +1238,7 @@ function startDownload($course, courseData, subTitle = "") {
1266
1238
} ) . then ( ( ) => {
1267
1239
resetCourse ( $course , $course . find ( ".download-error" ) , Settings . download . autoRetry , courseData , subtitle ) ;
1268
1240
} ) . catch ( ( error ) => {
1269
- const statusCode = error . response ?. status || 0 ;
1270
- const errorCode = error . code ? ` :${ error . code } ` : "" ;
1271
- appendLog ( `downloadLecture_Error: (${ statusCode } ${ errorCode } )` , error ) ;
1241
+ handleApiError ( error , "EDL_DOWNLOADING_LECTURE" , courseData . name , false ) ;
1272
1242
1273
1243
try {
1274
1244
if ( statusCode == 401 || statusCode == 403 ) {
@@ -1296,7 +1266,7 @@ function startDownload($course, courseData, subTitle = "") {
1296
1266
if ( hasDRMProtection ( dl ) ) {
1297
1267
dl . emit ( "end" ) ;
1298
1268
} else {
1299
- appendLog ( "errorDownload " , dl . error . message ) ;
1269
+ appendLog ( "DL_ONERROR " , dl . error . message ) ;
1300
1270
}
1301
1271
} ) ;
1302
1272
@@ -1779,11 +1749,13 @@ function clearBagdeLoggers() {
1779
1749
* @param {string } title - The title of the log entry.
1780
1750
* @param {string|Error|object } error - The error message or Error object.
1781
1751
*/
1782
- function appendLog ( title , error ) {
1783
- const description = error instanceof Error
1784
- ? error . message
1752
+ function appendLog ( title , error , additionalDescription = "" ) {
1753
+ let description = error instanceof Error
1754
+ ? error . message //`${error.message}\n ${error.stack}`
1785
1755
: ( typeof error == "object" ? JSON . stringify ( error ) : error ) ;
1786
1756
1757
+ description += additionalDescription !== "" ? "\n\n" + additionalDescription : "" ;
1758
+
1787
1759
// item added to list to display
1788
1760
$ ( ".ui.logger.section .ui.list" ) . prepend (
1789
1761
`<div class="item">
@@ -1810,6 +1782,7 @@ function appendLog(title, error) {
1810
1782
1811
1783
if ( error instanceof Error ) {
1812
1784
console . error ( `[${ title } ] ${ error . message } \n ${ error . stack } ` ) ;
1785
+ captureException ( error ) ;
1813
1786
} else {
1814
1787
console . warn ( `[${ title } ] ${ description } ` ) ;
1815
1788
}
@@ -1846,6 +1819,45 @@ function saveLogFile() {
1846
1819
} ) ;
1847
1820
}
1848
1821
1822
+ function handleApiError ( error , errorName , courseName = null , triggerThrow = true ) {
1823
+ error . name = errorName ;
1824
+ error . code = error . code || "" ;
1825
+
1826
+ const statusCode = error . response ?. status || 0 ;
1827
+ switch ( statusCode ) {
1828
+ case 403 :
1829
+ error . message = translate ( "You do not have permission to access this course" ) ;
1830
+ // prompt.alert(msgError);
1831
+ showAlertError ( error . message , errorName ) ;
1832
+ break ;
1833
+ case 503 :
1834
+ error . message = translate ( "Service is temporarily unavailable. Please wait a few minutes and try again." ) ;
1835
+ showAlertError ( error . message , errorName ) ;
1836
+ break ;
1837
+ case 504 :
1838
+ error . message = "Gateway timeout" ;
1839
+ showAlertError ( error . message , errorName ) ;
1840
+ break ;
1841
+ default :
1842
+ break ;
1843
+ }
1844
+
1845
+ if ( courseName )
1846
+ error . message += `\n\n course: ${ courseName } ` ;
1847
+
1848
+ appendLog ( `${ errorName } : ${ error . code } (${ statusCode } )` , error ) ;
1849
+
1850
+ if ( triggerThrow ) {
1851
+ // throw utils.newError(errorName, error.message);
1852
+ throw error ;
1853
+ }
1854
+ }
1855
+
1856
+ function showAlertError ( message , title = "" ) {
1857
+ title = title ? `.:: ${ title } ::.` : ".:: Error ::." ;
1858
+ dialog . showErrorBox ( title , message ) ;
1859
+ }
1860
+
1849
1861
function showAlert ( message , title = "" ) {
1850
1862
if ( title ) title = `.:: ${ title } ::.\n\r` ;
1851
1863
dialogs . alert ( `${ title } ${ message } ` ) ;
@@ -1856,14 +1868,14 @@ function captureException(exception) {
1856
1868
}
1857
1869
1858
1870
process . on ( "uncaughtException" , ( error ) => {
1859
- appendLog ( "uncaughtException " , error ) ;
1871
+ appendLog ( "EPROCESS_UNCAUGHT_EXCEPTION " , error ) ;
1860
1872
captureException ( error ) ;
1861
1873
} ) ;
1862
1874
1863
1875
process . on ( "unhandledRejection" , ( error ) => {
1864
- appendLog ( "unhandledRejection " , error ) ;
1876
+ appendLog ( "EPROCESS_UNHANDLED_REJECTION " , error ) ;
1865
1877
captureException ( error ) ;
1866
1878
} ) ;
1867
1879
1868
1880
// console.table(getAllDownloadsHistory());
1869
- checkLogin ( ) ;
1881
+ checkLogin ( false ) ;
0 commit comments