Skip to content

Commit

Permalink
Improve handling of json parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wkmor1 committed Jan 21, 2025
1 parent 392059d commit 1159243
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 20 deletions.
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# YAML 1.2
---
version: "0.9.9.9000"
date-released: 2024-11-05
date-released: 2025-01-21
title: finbif
abstract: |
"The `finbif` R package is a programmatic interface to the [Finnish
Expand Down Expand Up @@ -49,7 +49,7 @@ preferred-citation:
orcid: "https://orcid.org/0000-0002-8686-4154"
title: Introduction to the finbif package
type: manual
year: 2024
year: 2025
doi: "10.5281/zenodo.3612814"
notes: "R package version 0.9.9.9000"
url: "https://finbif-docs-dev.netlify.app/articles/finbif.html"
Expand Down
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Depends: R (>= 3.5.0)
Imports:
digest,
httr,
jsonlite,
lutz,
utils
Roxygen: list(markdown = TRUE)
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
YEAR: 2019-2024
YEAR: 2019-2025
COPYRIGHT HOLDER: LUOMUS - Finnish Museum of Natural History
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License

Copyright (c) 2019-2024 LUOMUS - Finnish Museum of Natural History
Copyright (c) 2019-2025 LUOMUS - Finnish Museum of Natural History

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ importFrom(httr,RETRY)
importFrom(httr,content)
importFrom(httr,progress)
importFrom(httr,write_disk)
importFrom(jsonlite,validate)
importFrom(lutz,tz_lookup_coords)
importFrom(utils,packageVersion)
importFrom(utils,read.delim)
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# finbif 0.9.9.9000

- This is the current development version.
- Improvements have been made to the way errors in parsing json are handled.

# finbif 0.9.9

Expand Down
18 changes: 14 additions & 4 deletions R/api_get.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' @noRd
#' @importFrom digest digest
#' @importFrom httr content RETRY
#' @importFrom jsonlite validate
#' @importFrom utils packageVersion

api_get <- function(obj) {
Expand Down Expand Up @@ -244,21 +245,30 @@ api_get <- function(obj) {

resp[[c("request", "url")]] <- notoken

parsed <- httr::content(resp)
txt <- httr::content(resp, type = "text", encoding = "UTF-8")

if (!identical(resp[["status_code"]], 200L)) {
valid <- jsonlite::validate(txt)

if (!identical(resp[["status_code"]], 200L) || !valid) {

parsed <- httr::content(resp)

obj <- NULL

err_msg <- paste0(
"API request failed [", resp[["status_code"]], "]\n", parsed[["message"]]
"API request failed [",
resp[["status_code"]],
"]\n",
parsed[["message"]],
"\n",
txt
)

stop(err_msg, call. = FALSE)

}

obj[["content"]] <- parsed
obj[["content"]] <- httr::content(resp)

obj[["response"]] <- resp

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ FinBIF itself and the contributed datasets please visit this link:

To cite the finbif R package in publications please use:

Morris, William K. (2024). Introduction to the finbif package. R
package version 0.9.9, https://doi.org/10.5281/zenodo.3612814
Morris, William K. (2025). Introduction to the finbif package. R
package version 0.9.9.9000, https://doi.org/10.5281/zenodo.3612814

----

Expand Down
18 changes: 15 additions & 3 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@
"sameAs": "https://CRAN.R-project.org/package=httr"
},
"4": {
"@type": "SoftwareApplication",
"identifier": "jsonlite",
"name": "jsonlite",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=jsonlite"
},
"5": {
"@type": "SoftwareApplication",
"identifier": "lutz",
"name": "lutz",
Expand All @@ -212,7 +224,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=lutz"
},
"5": {
"6": {
"@type": "SoftwareApplication",
"identifier": "utils",
"name": "utils"
Expand All @@ -222,11 +234,11 @@
"applicationCategory": "Biodiversity",
"isPartOf": "https://species.fi",
"keywords": ["api", "biodiversity", "biodiversity-informatics", "biodiversity-information", "finbif", "finbif-access", "occurrences", "r-package", "r-programming", "rstats", "species", "specimens", "taxon", "taxonomy", "web-services"],
"fileSize": "12098.553KB",
"fileSize": "12101.633KB",
"citation": [
{
"@type": "SoftwareSourceCode",
"datePublished": "2024",
"datePublished": "2025",
"author": [
{
"@type": "Person",
Expand Down
2 changes: 1 addition & 1 deletion docs/LICENSE-text.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ articles:
v03_selecting_variables: v03_selecting_variables.html
v04_metadata: v04_metadata.html
v05_filtering: v05_filtering.html
last_built: 2025-01-09T12:46Z
last_built: 2025-01-21T09:30Z
urls:
reference: https://finbif-docs-dev.netlify.app/reference
article: https://finbif-docs-dev.netlify.app/articles
2 changes: 1 addition & 1 deletion docs/search.json

Large diffs are not rendered by default.

0 comments on commit 1159243

Please sign in to comment.