Skip to content

Commit 09a0d5d

Browse files
committed
Add key when warning for missing fields; closes #48
1 parent 7b17ef8 commit 09a0d5d

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

R/04InternalFunctions.R

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
if (length(rfields) > 0L) {
1515
ok <- vapply(rfields, function(f) any(f %in% fields), FALSE)
1616
if (any(!ok)){
17-
if (check == 'warn'){
18-
warning(sprintf(ngettext(sum(!ok),
19-
"A bibentry of bibtype %s has to specify the field: %s",
17+
key <- attr(x, "key")
18+
if (is.null(key))
19+
key <- ""
20+
else
21+
key <- paste0(key, ": ")
22+
msg <- sprintf(ngettext(sum(!ok),
23+
"%sA bibentry of bibtype %s has to specify the field: %s",
2024
"A bibentry of bibtype %s has to specify the fields: %s"),
21-
sQuote(bibtype), paste(rfields[!ok], collapse = ", ")),
22-
domain = NA)
25+
key, sQuote(bibtype), paste(rfields[!ok], collapse = ", "))
26+
if (check == 'warn'){
27+
warning(msg, domain = NA, call. = FALSE)
2328
return(NULL)
24-
}else{
25-
stop(sprintf(ngettext(sum(!ok),
26-
"A bibentry of bibtype %s has to specify the field: %s",
27-
"A bibentry of bibtype %s has to specify the fields: %s"),
28-
sQuote(bibtype), paste(rfields[!ok], collapse = ", ")),
29-
domain = NA)
30-
}
29+
}else
30+
stop(msg, domain = NA)
3131
}
3232
}
3333
}
@@ -940,14 +940,22 @@ MakeBibEntry <- function(x, to.person = TRUE){
940940
if (type != 'set')
941941
tdate <- ProcessDates(y)
942942

943-
tryCatch(BibEntry(bibtype = type, key = key, dateobj = tdate, other = y),
943+
withCallingHandlers(tryCatch(BibEntry(bibtype = type, key = key,
944+
dateobj = tdate, other = y),
944945
error = function(e){
945-
message(sprintf("Ignoring entry '%s' %sbecause:\n\t%s\n",
946+
message(sprintf("Ignoring entry '%s' %sbecause:\n\t%s",
946947
key,
947948
line.no,
948-
conditionMessage(e)))
949+
conditionMessage(e)), domain = NA)
949950
NULL
950-
})
951+
}),
952+
warning = function(w){
953+
warning(sprintf("%s %s:\n\t%s",
954+
key,
955+
line.no,
956+
conditionMessage(w)), domain = NA, call. = FALSE)
957+
invokeRestart("muffleWarning")
958+
})
951959
}
952960

953961
#' @keywords internal

inst/NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ which defaults to `BibOptions()$ignore.case` (`TRUE`) so that case is
88
ignore when checking for duplicate fields (h/t Justin Calabrese #47)
99
* Improved warning message when printing entries with unknown
1010
LaTeX macros (the entry key is now included). (h/t Justin Calabrese #49)
11+
* The entry key is now included in warning messages when entries are
12+
missing fields and `BibOptions()$check.entries == "warn"` (h/t Justin
13+
Calabrese #48)
1114

15+
BUG FIXES
16+
17+
* Entries are now only checked once to ensure all required
18+
fields are present in `ReadBib`
1219

1320
Changes in Version 1.1.0 (2018-04-02)
1421
--------------------------------------------------------

0 commit comments

Comments
 (0)