Skip to content

Scripting: Handle Errors

ens-bwalts edited this page Feb 5, 2020 · 1 revision

You can use these examples to handle errors.

Error Handling – Python

You should never assume that your request has worked.


import requests, sys
server = "https://rest.ensembl.org"
ext = "/lookup/id/ENSG00000157764?expand=1"
r = requests.get(server+ext, headers={ "Accept" : "application/json"})
if not r.ok:
r.raise_for_status()

Check the response code returned by the server.

Error Handling – R

You should never assume that your request has worked.


library(httr)
library(jsonlite)
server <- "https://rest.ensembl.org"
ext <- "/lookup/id/ENSG00000157764"
r <- GET(paste(server, ext, sep = ""), content_type("application/json"))
r
stop_for_status(r)

Check the response code returned by the server.

Error Handling – Perl

You should never assume that your request has worked.


use HTTP::Tiny;
use JSON;
my $http = HTTP::Tiny->new();
my $server = "https://rest.ensembl.org";
my $ext = "/lookup/id/ENSG00000157764";
my $headers = { 'Content-Type' => 'application/json' };
my $r = $http->get($server.$ext, {headers => $headers});
die $r->{status}, "\n" unless $r->{success};

Check the response code returned by the server.