New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update feature detection example #341
Update feature detection example #341
Conversation
@kenchris to also review the feature detection example code. |
} else if (error.name === 'ReferenceError') { | ||
console.log('Sensor is not supported by the User Agent.'); | ||
} else { | ||
throw error; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is throwing considered here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there are other errors that handled in the if / else blocks, I think it is better to re-throw them, rather than supressing or logging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
throwing doesn't make it robust though, an error happened and what ever happened you handled it... throwing can end up affecting other systems, like if someone else ends up catching it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is outside of scope of this example. Robust application should not suppress error propagation to avoid unpredictable behavior. I can fix throwing from error handler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
index.bs
Outdated
accelerometer.addEventListener('error', event => handleErrors(event.error)); | ||
accelerometer.addEventListener('reading', () => reloadOnShake(accelerometer)); | ||
accelerometer.start(); | ||
} catch (error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe comment like // Handle construction errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
index.bs
Outdated
let accelerometer = null; | ||
try { | ||
accelerometer = new Accelerometer({ frequency: 10 }); | ||
accelerometer.addEventListener('error', event => handleErrors(event.error)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe comment // handle runtime errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens when handleErrors throws from a runtime error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log to console :) unhandled exception
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to add that as a comment above the throw
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then the throw makes even less sense... why not print to console straight away in both cases, you could use console.error()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added comment for runtime errors.
index.bs
Outdated
handleErrors(error); | ||
} | ||
|
||
function handleErrors(error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its called handleErrors but not doing much handling :-) more like logging
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it is an example and not a full blown app :) Do you have some suggestions? It is app specific, to define behavior for particular error condition. E.g, fallback to DevMotion, show notification, etc.
index.bs
Outdated
} | ||
|
||
function handleErrors(error) { | ||
if (error.name === 'NotAllowedError') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Do proper error handling here, like show notification, or fallback to other sensor;
// whatever makes the most sense for your application scenario.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added text to example description.
bdae54b
to
45476e6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
45476e6
to
00b67d5
Compare
Fixes: #339
Preview | Diff