Skip to content

Commit af51675

Browse files
committed
add prop onError to handle request errors
1 parent 55f1e05 commit af51675

File tree

10 files changed

+124
-68
lines changed

10 files changed

+124
-68
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Installing this component is very easy and it has just one dependency: [React](h
2020
$ bower install --save react-video
2121
```
2222

23-
- Or if you want to [download the lastest release](https://github.com/pedronauck/react-video/archive/v1.4.0.zip) and put in your website, it will work too!
23+
- Or if you want to [download the lastest release](https://github.com/pedronauck/react-video/archive/v1.5.0.zip) and put in your website, it will work too!
2424

2525
**NOTICE:** You need just one thing to make the component work. Put the [base component style](./dist/react-video.css) at the `<header>` tag. If you don't wanna use the `.css` extension, you can get the `.styl` or `.scss` extension at the folder `./lib`.
2626

@@ -55,6 +55,19 @@ The property `videoId` is optional, so you can use it or not. If you don't pass
5555
);
5656
```
5757

58+
To handle errors when something happens, like your video can't be loaded, you can pass a callback with a prop `onError` in the component:
59+
60+
```javascript
61+
var _onError = function(err) {
62+
console.log(err);
63+
};
64+
65+
React.render(
66+
<Video onError={_onError} videoId={videoId} />
67+
document.querySelector('#your-div')
68+
);
69+
```
70+
5871
If you decide to use just Javascript without any module loader, you can get the global variable `window.ReactVideo` *(or just `ReactVideo`)*:
5972

6073
```javascript
@@ -97,6 +110,7 @@ Property | Type | Default | Required | Description
97110
-------- | ---- | ------- | -------- |-----------
98111
from | `String` | none | no | Video source: `youtube` or `vimeo`. Leave empty and the service will be detected for you by looking a the id.
99112
videoId | `String` | none | no | The video ID
113+
onError | `Function` | yes | no | Callback function if the video can't be loaded
100114

101115
## Contributing
102116

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-video",
3-
"version": "1.4.0",
3+
"version": "1.5.0",
44
"description": "React component to load video from Vimeo or Youtube across any device",
55
"homepage": "https://github.com/pedronauck/react-video",
66
"authors": [

dist/react-video.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* React Video - React component to load video from Vimeo or Youtube across any device
3-
* @version v1.4.0
3+
* @version v1.5.0
44
* @link https://github.com/pedronauck/react-video
55
* @license MIT
66
* @author Pedro Nauck (https://github.com/pedronauck)

dist/react-video.js

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* React Video - React component to load video from Vimeo or Youtube across any device
3-
* @version v1.4.0
3+
* @version v1.5.0
44
* @link https://github.com/pedronauck/react-video
55
* @license MIT
66
* @author Pedro Nauck (https://github.com/pedronauck)
@@ -72,7 +72,8 @@ return /******/ (function(modules) { // webpackBootstrap
7272
displayName: 'Video',
7373
propTypes: {
7474
from: React.PropTypes.oneOf(['youtube', 'vimeo']),
75-
videoId: React.PropTypes.string.isRequired
75+
videoId: React.PropTypes.string.isRequired,
76+
onError: React.PropTypes.func
7677
},
7778
getDefaultProps:function() {
7879
return {
@@ -147,28 +148,34 @@ return /******/ (function(modules) { // webpackBootstrap
147148
},
148149
fetchYoutubeData:function() {
149150
var id = this.props.videoId;
150-
var url = ("//gdata.youtube.com/feeds/api/videos/" + id + "?v=2&alt=json");
151151

152-
ajax.get(url, function(err, res) {
153-
var gallery = res.entry['media$group']['media$thumbnail'];
154-
var thumb = gallery.sort(function(a, b) {return b.width - a.width;})[0].url;
155-
156-
this.setState({
157-
thumb: thumb,
158-
imageLoaded: true
159-
});
160-
}.bind(this));
152+
ajax.get({
153+
url: ("//gdata.youtube.com/feeds/api/videos/" + id + "?v=2&alt=json"),
154+
onSuccess:function(err, res) {
155+
var gallery = res.entry['media$group']['media$thumbnail'];
156+
var thumb = gallery.sort(function(a, b) {return b.width - a.width;})[0].url;
157+
158+
this.setState({
159+
thumb: thumb,
160+
imageLoaded: true
161+
})
162+
},
163+
onError: this.props.onError
164+
});
161165
},
162166
fetchVimeoData:function() {
163167
var id = this.props.videoId;
164-
var url = ("//vimeo.com/api/v2/video/" + id + ".json");
165-
166-
ajax.get(url, function(err, res) {
167-
this.setState({
168-
thumb: res[0].thumbnail_large,
169-
imageLoaded: true
170-
});
171-
}.bind(this));
168+
169+
ajax.get({
170+
url: ("//vimeo.com/api/v2/video/" + id + ".json"),
171+
onSuccess:function(err, res) {
172+
this.setState({
173+
thumb: res[0].thumbnail_large,
174+
imageLoaded: true
175+
});
176+
},
177+
onError: this.props.onError
178+
});
172179
}
173180
});
174181

@@ -202,43 +209,50 @@ return /******/ (function(modules) { // webpackBootstrap
202209
/* 3 */
203210
/***/ function(module, exports, __webpack_require__) {
204211

205-
/** @jsx React.DOM */var get = function(url, cb) {
212+
/** @jsx React.DOM */exports.get = function(opts) {
213+
var url = opts.url;
214+
var successCb = opts.onSuccess;
215+
var errorCb = opts.onError;
206216
var req = false;
207217

208218
// XDomainRequest onload
209-
var oldIE = function () {
210-
cb(null, JSON.parse(req.responseText));
219+
var _oldIE = function () {
220+
successCb(null, JSON.parse(req.responseText));
211221
};
212222

213223
// XMLHttpRequest onload
214-
var onLoad = function () {
224+
var _onLoad = function () {
215225
if (req.readyState !== 4) return;
216-
if (req.status === 200) cb(null, JSON.parse(req.responseText));
226+
if (req.status === 200) successCb(null, JSON.parse(req.responseText));
217227
else {
218-
cb({ error: 'Sorry, an error ocurred on the server' }, null);
228+
var err = { error: 'Sorry, an error ocurred on the server' };
229+
230+
if (errorCb && typeof errorCb === 'function') return errorCb(err);
231+
successCb(err, null);
219232
}
220233
};
221234

222-
var onError = function() {
223-
cb({ error: 'Problem with your internet conection' }, null);
235+
var _onError = function() {
236+
var err = { error: 'Sorry, an error ocurred on the server' };
237+
238+
if (errorCb && typeof errorCb === 'function') return errorCb(err);
239+
successCb(err, null);
224240
};
225241

226242
try {
227243
req = new XDomainRequest();
228-
req.onload = oldIE;
244+
req.onload = _oldIE;
229245
}
230246
catch (e) {
231247
req = new XMLHttpRequest();
232-
req.onreadystatechange = onLoad;
248+
req.onreadystatechange = _onLoad;
233249
}
234250

235-
req.onerror = onError;
251+
req.onerror = _onError;
236252
req.open('GET', url, true);
237253
req.send();
238254
};
239255

240-
module.exports = { get: get };
241-
242256

243257
/***/ },
244258
/* 4 */

dist/react-video.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/react-video.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ <h2 class="section-title">Usage</h2>
9999
document.querySelector('#your-div')
100100
);
101101
</code></pre>
102+
<p>
103+
To handle errors when something happens, like your video can't be loaded, you can pass a callback with a prop <code>onError</code> in the component:
104+
105+
<pre><code class="hljs javascript">
106+
var _onError = function(err) {
107+
console.log(err);
108+
};
109+
110+
React.render(
111+
&lt;Video onError={_onError} videoId={videoId} /&gt;
112+
document.querySelector('#your-div')
113+
);
114+
</code></pre>
115+
</p>
102116
<p>
103117
If you decide to use just Javascript without any module loader, you can get the global variable <code>window.ReactVideo</code>:
104118
</p>

lib/react-video.jsx

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ module.exports = React.createClass({
88
displayName: 'Video',
99
propTypes: {
1010
from: React.PropTypes.oneOf(['youtube', 'vimeo']),
11-
videoId: React.PropTypes.string.isRequired
11+
videoId: React.PropTypes.string.isRequired,
12+
onError: React.PropTypes.func
1213
},
1314
getDefaultProps() {
1415
return {
@@ -83,27 +84,33 @@ module.exports = React.createClass({
8384
},
8485
fetchYoutubeData() {
8586
var id = this.props.videoId;
86-
var url = `//gdata.youtube.com/feeds/api/videos/${id}?v=2&alt=json`;
8787

88-
ajax.get(url, (err, res) => {
89-
var gallery = res.entry['media$group']['media$thumbnail'];
90-
var thumb = gallery.sort((a, b) => b.width - a.width)[0].url;
88+
ajax.get({
89+
url: `//gdata.youtube.com/feeds/api/videos/${id}?v=2&alt=json`,
90+
onSuccess(err, res) {
91+
var gallery = res.entry['media$group']['media$thumbnail'];
92+
var thumb = gallery.sort((a, b) => b.width - a.width)[0].url;
9193

92-
this.setState({
93-
thumb: thumb,
94-
imageLoaded: true
95-
});
94+
this.setState({
95+
thumb: thumb,
96+
imageLoaded: true
97+
})
98+
},
99+
onError: this.props.onError
96100
});
97101
},
98102
fetchVimeoData() {
99103
var id = this.props.videoId;
100-
var url = `//vimeo.com/api/v2/video/${id}.json`;
101104

102-
ajax.get(url, (err, res) => {
103-
this.setState({
104-
thumb: res[0].thumbnail_large,
105-
imageLoaded: true
106-
});
105+
ajax.get({
106+
url: `//vimeo.com/api/v2/video/${id}.json`,
107+
onSuccess(err, res) {
108+
this.setState({
109+
thumb: res[0].thumbnail_large,
110+
imageLoaded: true
111+
});
112+
},
113+
onError: this.props.onError
107114
});
108115
}
109116
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-video",
3-
"version": "1.4.0",
3+
"version": "1.5.0",
44
"description": "React component to load video from Vimeo or Youtube across any device",
55
"author": {
66
"name": "Pedro Nauck",

utils/ajax.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
1-
var get = function(url, cb) {
1+
exports.get = function(opts) {
2+
var url = opts.url;
3+
var successCb = opts.onSuccess;
4+
var errorCb = opts.onError;
25
var req = false;
36

47
// XDomainRequest onload
5-
var oldIE = function () {
6-
cb(null, JSON.parse(req.responseText));
8+
var _oldIE = function () {
9+
successCb(null, JSON.parse(req.responseText));
710
};
811

912
// XMLHttpRequest onload
10-
var onLoad = function () {
13+
var _onLoad = function () {
1114
if (req.readyState !== 4) return;
12-
if (req.status === 200) cb(null, JSON.parse(req.responseText));
15+
if (req.status === 200) successCb(null, JSON.parse(req.responseText));
1316
else {
14-
cb({ error: 'Sorry, an error ocurred on the server' }, null);
17+
var err = { error: 'Sorry, an error ocurred on the server' };
18+
19+
if (errorCb && typeof errorCb === 'function') return errorCb(err);
20+
successCb(err, null);
1521
}
1622
};
1723

18-
var onError = function() {
19-
cb({ error: 'Problem with your internet conection' }, null);
24+
var _onError = function() {
25+
var err = { error: 'Sorry, an error ocurred on the server' };
26+
27+
if (errorCb && typeof errorCb === 'function') return errorCb(err);
28+
successCb(err, null);
2029
};
2130

2231
try {
2332
req = new XDomainRequest();
24-
req.onload = oldIE;
33+
req.onload = _oldIE;
2534
}
2635
catch (e) {
2736
req = new XMLHttpRequest();
28-
req.onreadystatechange = onLoad;
37+
req.onreadystatechange = _onLoad;
2938
}
3039

31-
req.onerror = onError;
40+
req.onerror = _onError;
3241
req.open('GET', url, true);
3342
req.send();
3443
};
35-
36-
module.exports = { get: get };

0 commit comments

Comments
 (0)