Skip to content

Commit

Permalink
Better SoundCloud error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cookpete committed Jan 26, 2016
1 parent f594965 commit bce57af
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/players/SoundCloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,27 @@ export default class SoundCloud extends Base {
}
getSongData (url) {
return fetch(RESOLVE_URL + '?url=' + url + '&client_id=' + this.props.soundcloudConfig.clientId)
.then(response => response.json())
.then(response => {
if (response.status >= 200 && response.status < 300) {
return response => response.json()
} else {
const error = new Error(response.statusText)
error.response = response
throw error
}
})
}
load (url) {
this.stop()
this.getSDK().then(SC => {
this.getSongData(url).then(data => {
if (url !== this.props.url) return // Abort if url changes during async requests
if (url !== this.props.url) {
return // Abort if url changes during async requests
}
if (!data.streamable) {
this.props.onError(new Error('SoundCloud track is not streamable'))
return
}
const image = data.artwork_url || data.user.avatar_url
if (image) {
this.setState({ image: image.replace('-large', '-t500x500') })
Expand All @@ -55,8 +69,8 @@ export default class SoundCloud extends Base {
player._player.on('stateChange', this.onStateChange)
this.onReady()
})
})
})
}, this.props.onError)
}, this.props.onError)
}
onStateChange = state => {
if (state === 'playing') this.onPlay()
Expand Down

0 comments on commit bce57af

Please sign in to comment.