Skip to content

Commit

Permalink
Feature: API error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fvdm committed Dec 16, 2023
1 parent 062a5d4 commit 2f8cfd3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions searchitunes.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ module.exports = async function SearchItunes ( {
const res = await fetch( url, options );
const data = await res.json();

// API error
if ( data.errorMessage ) {
throw new Error( `API: ${data.errorMessage}` );
}

// Empty result
if ( ! data.results || ! data.results.length ) {
throw new Error( 'no results' );
Expand Down
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,28 @@ dotest.add( 'Default timeout', async test => {
} );


dotest.add( 'API error', async test => {
let error;
let data;

try {
data = await app( {
id: 'error-test',
} );
}
catch ( err ) {
error = err;
}

test()
.isError( 'fail', 'error', error )
.isRegexpMatch( 'fail', 'error.message', error?.message, /^API: .+/ )
.isUndefined( 'fail', 'data', data )
.done()
;
} );


// Start the tests
dotest.run();

0 comments on commit 2f8cfd3

Please sign in to comment.