Comments (3)
if the line fails, consider filing a bug report / issue. If it is a permission issue, sharing troubleshooting helps others. If its a system level restriction or other non-library related issue it helps expand the collective knowledge. And in the case is a defect or better error reporting / behavior, that may have a future as a PR into this lib :)
(some opinion):
- Testing for construction is usually a code smell.
- And try/catch-ing breaks some best practices but mainly, is confusing to developers (though commonly used)
Constructors should returnthis
object regardless. And provide andisValid
-like method for testings construction state.
(end)
TL;DR; ya, try / catch at some level will solve things 🤦♂️
There are also guards that can be used. Gpio.accessible
(static method on class - aka, can be called before any new
) ... will attempt to test the basic access levels needed for this library to function (though, it does't say that it will succeed later, just that the core interface (sysfs) /sys/class/gpio/export
is available to the running process (aka, can we call sysfs gpio/export write number
).
A test like this only provides a useful guard for system that do not process a /sys/class/gpio
or even device tree itself (...windows, and some container/vm restrictions etc).
Constructor Behavior
The Gpio
constructors normal throw
path is related to permission:
Line 157 in 07d36e2
makes a call into waitForGpioAccessPermission
which add addition construction time constraints (access to the gpios specific sysfs files).
Which eventually can throw:
Line 73 in 07d36e2
There are other theoretical exceptions that can plague constructors also ... Buffer.alloc
A TypeError will be thrown if size is not a number. NodeJS Buffer
though you have a better chance at winning the lottery.
A control parameter gpioPreviouslyExported
:
Line 81 in 07d36e2
can be used to alter the behavior within limited scope (reducing the need for the current runtime to need access to the sysfs
gpio/export
. This requires coordinated orchestration between consumers however (but can also be used to subvert systems that apply restrictions on the export and / or also add user permission into).
A more comprehensive accessCheck
has been considered / discussed but likely falls a bit outside scope. There may be space for an npm helper module. SysFsAccess
or some such, but a library as such would spill into the non-gpio land quickly as a generic solution and add bulk to this lib.
In practice
The odds are, permission or miss-configuration is the likely issue. Guarding against this at the Gpio
level is likely too fine grained and should be moved into an application-setup phase. For other error, allowing them to bubble (by not adding try/catchs) up to the application layer is likely the correct logic.
Wrapping them in Promise
s can also provide a standard catch
workflow that can be useful.
Other errors that exist around the Epoll
dependency and unsupported system. This usually manifests itself as a require(onoff)
related exception, though it can also cause a undefined
exception when trying to use the class.
As an aside, no try/catch specific blocks are usually added to code I write, allowing for the highest level application to execute proper fallback handling or error. 10 out of 10 times, the error has been configuration or logic error on my end :)
from onoff.
Wow, thanks for the great detail. Since I am developing for Node Red, I usually setup a try/catch wrapper around any library calls or dependencies, this is more to isolate exceptions from the parent Node Red framework, which is a best practice, since a node failing should not be allowed to crash Node Red. I have not yet used Promise methodology (much) in that I am new to Node(js) or JavaScript, but the concept I do understand, so as I develop more elegant code, I plan to move towards more Promise leveraging.
from onoff.
@johntalton Thank you for providing all the useful feedback.
@Nodi-Rubrum I'll go ahead and close this issue as I think all the required information has been provided.
from onoff.
Related Issues (20)
- Problem using watch function with relay HOT 10
- Initialize GPIO without change pin status. HOT 6
- GPIO not working in a worker_thread - nodejs 14.15 HOT 2
- EACCES: permission denied, open '/sys/class/gpio/export' as superuser HOT 2
- Cannot run on non Rpi system HOT 1
- TypeError: writeSync is not a function HOT 4
- Raspberry Pi GPIO control in electron App HOT 8
- My computer does not have a /sys/class/gpio directory, but has /dev/gpiochip* HOT 1
- Interrupt misfire HOT 3
- Raspberry 4 - malloc(): corrupted top size HOT 3
- Error: EPERM: operation not permitted, write HOT 4
- Does not work with Compute Module 4. HOT 2
- Event is fired multiple times
- Superb library - I am running on Node v20 HOT 2
- Pi 5 Compatibility issues HOT 9
- Compatibility with ppp asked?
- RPI 5 gpio numbers HOT 1
- fs error HOT 6
- Newer version of epoll
- Button.watch spams endlessly HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from onoff.