-
Notifications
You must be signed in to change notification settings - Fork 2
/
Error handling with tryCatch
54 lines (44 loc) · 1.29 KB
/
Error handling with tryCatch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
###Function for automatic error handling while downloading Facebook pages, posts, comments and replies with Rfacebook library###
### The code was developed as part of the Ancient Identities in Modern Britain (IARH) project
### Project: Ancient Identities in Modern Britain (IARH); ancientidentities.org
### Author: Marta Krzyzanska
###Requires defining variables:
limitError <-"(#17) User request limit reached"
unexpectedError<-"An unexpected error has occurred. Please retry your request later."
###Requires libraries:
library(Rfacebook)
library(devtools)
library(xlsx)
handleAPIErrors <- function(id,token,type,n){
z=1
y=2
while(z<y){
z=z+1
content<-
tryCatch(
if(type=="pages"){
searchPages(string=id, token=token, n=n)}
else if(type=="posts"){
getPage(page=id, token=token, n=n)}
else if(type=="comments"){
getPost(post=id,n=n,token=token)}
else if(type=="replies"){
getCommentReplies(id,n=n,token=token)}
,
error=function(error_message){
message(error_message)
error_message<-append(error_message,id)
if(error_message$message==limitError){
Sys.sleep(900)
return (1)
}else if(error_message$message==unexpectedError){
return (1)}else{
return(error_message)
z=y}
}
)
if(class(content)=="numeric"){
z=1}
}
return (content)
}