-
Notifications
You must be signed in to change notification settings - Fork 102
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
Switch-D365ActiveDatabase should generate Error instead of Warning #265
Comments
Thanks for the suggestion 😀 The current implementation is the preferred approach, so we don't have errors, exceptions and other things contaminating the console. We actually don't want to throw exceptions, because it breaks execution, as you expect. This can be hard / difficult to work around when first starting on that path, but when you first get used to it, you don't wanna go back. I would propose that you do the following:
|
Thanks for the details! I can't agree with you. I think you have the wrong strategy. This behavior may get into the data loss or wrong data on the server and nobody mentions it. Could you just add MORE RED colors on this message? |
@valerymoskalenko Just add the additional parameter
Output of that script:
|
@valerymoskalenko I appreciate you being honest, and stating that you don't agree with the decision. So thanks for that! On a personal note, everybody that skips in with work and hours, gets a saying in where the module should move, including the direction on how we should handle errors vs. warnings. On to the issue at hand, I just tested @devax and his suggestion. He is totally rigt, the result is the an exception like you want things to work. Thing is, I did not implement Write-Warning - so right now, things will not work for you, as expected. But this is a learning experience for all of us. I propose 2 possible solutions, that requires some work, before it is implemented across the entire module.
Suggestion 2 actually seems to be the best fit, because you centrally and only once, decide how you want things to work for you. On the color of things, I need to document how you change the different colors using the Let's continue the discussion, before I start refactoring things. I could totally use some help with the refactor. I can create and document the first cmdlet / function, and then you guys could help out making the changes. Let me know what you think, both of you! @valerymoskalenko @devax |
@Splaxi Thank you for looking into this! I try to add something to the discussion. IMHO, suggestion 1 should be implemented, of course only there, where you actually want to output a warning. This seems to be independent of the issue described in this topic. I have not realized that you are using Stop-PSFunction, so I had an other look at that. I can confirm, Stop-PSFunctions seems not to respect the -WarningAction value of the current function it is running in. The core question of the discussion seems to be: Is a warning appropriate in this situation or would an Error be more adequate? Like @valerymoskalenko has already stated, this is debatable and everyone might interpret it differently. I tend to agree with @valerymoskalenko: when I call a function named
Output:
|
@devax Thank you for taking the time to share your ideas and help me understand things better. Your examples are a great help in the discussion! Let me start with explaining the idea behind the module, it may not bring anything new to the table, but I feel it is important in this discussion, because it is so fundamental. The core idea is to help people complete their "daily" tasks easier and more efficient, in a structured and repeatable manner. I want people to be able to produce the same output or results, regardless of their skill level and make them capable of predicting how long a given task will take. I choose PowerShell as the platform for several reasons.
The idea is that the tools help and guide people when they start using it, making sure that they don't get a "sea of red", PowerShell community lingo for exceptions, that scares them off. The exceptions don't provide any guidance or explaining what went wrong. It actually takes a lot of effort analyze and understand a PowerShell exception, especially for people that haven't been using PowerShell much - heck, I still struggle with understanding PowerShell exceptions. So - this is why the current implementation is how it is. The module is split in being a helping hand for a person in his daily life, and along way help automate and produce repeatable results. The automation part is an added benefit, that comes from using PowerShell. Back to the discussion! Thanks to you @devax, I learned about I'm very depended on the PSFramework module, which I don't understand fully, but seek advice from the mastermind behind it all the time. Which brings me back to a simple solution that he shared with me
This should ensure that So, to sum up what solutions we have, to enable you guys get the desired exception handling is:
These things don't require any code change, and will make sure that we all get a tool that can help us in our daily life. @valerymoskalenko Please join the discussion when you get time on your hands, so we can make sure we cover all the different aspects. Finally - thank you both for caring enough about the module, to give some feedback. Without feedback, I'm just building things the way I like them to be 😉 Updated: I just "documented" how to get the desired exception flow on the wiki, so we have a starting point. https://github.com/d365collaborative/d365fo.tools/wiki/Exception-handling |
Thank you @Splaxi for laying out the core ideas behind the module. Your 4 reasons for choosing Power Shell seem very valid to me and I think PS is the most fitting platform for the task. Also, you have created something very useful that I'm using nearly daily, so thank you for that. I agree with everything you said, except for this part:
Going into details:
I don't think turning errors into warnings is a viable strategy to approach this problem. What people want is that "things work" when they use a command. This means to do everything you can to prevent that things go wrong. Example: I see you using a lot of default values for parameters. This is great and I consider this to be an adequate approach for this demand. Changing errors/exceptions into warnings on the other hand is not an adequate approach to this demand IMHO. When things go wrong, a user should be informed that things went wrong and the appropriate way to inform the user is to use errors/exceptions (at least in PS). Using warnings instead of an error does not make things "work better".
I must disagree here. A lot of effort went into the creation of meaningful exception messages that are very useful to the user. Personally, I consider Power Shell to be one of the most exemplary environments when it comes to error messages. Nearly daily I come across well written error messages in Power Shell, I could easily back this up with examples, if you want me to do so. Of course there are always the exceptions. But it is a new idea to me that people consider Power Shell error messages to be not easy to understand in general. In addition: Turning an error message into a warning message does not make it better readable or understandable, IMHO. Also, technically, every message presented as a warning message could also be presented as an error message. The concept of terminating and non terminating errors might be new to new Power Shell users and also the tools you have at hand like the ErrorAction parameter for commandlets and the $ErrorActionPreference variable need some initial effort to familiarise yourself with. Once mastered, it gives you a lot of control over your tools. I think any Power Shell module should try to leverage the design principles behind these tools instead of trying to make up their own, differing design principles. But that's just my opinion, everyone can have his/her own. ;) Coming back to the problem at hand:
Sorry this became such a long post. |
Hi Folks, jumping in on this discussion :) With that, I try to govern the default behavior I use depending on the target audience: If it is something likely to be consumed by beginners, then it writes warnings by default and rarely writes errors. The important thing to keep in mind is that if you default to warnings, you need to have a way to turn on errors instead so try/catch and similar tools can be used. I suppose I should find a way to offer better docs on that and make it more intuitive to implement (One of the core reasons for To make opting into exceptions possible however, you need to implement that toggle and pass it through. The basic idea is ...
[switch]
$EnableException And then either ...
Stop-PSFFunction -Message "Foo" -EnableException $EnableException or
which requires your module to opt into implicit inheritance by running the following line once during import: Set-PSFFeature -Name PSFramework.InheritEnableException -Value $true -ModuleName 'd365fo.tools' (Note: For implicit inheritance to work, your toggle parameter must be named Now, you can agree or disagree with this design decision, I'll stick with the argument in favor though. Btw, all messages - including error records that were attached - can be read after the fact by using Cheers, |
Hey, The issue is not dead and I'm going to implement a helper function for you guys to activate as the first step of your scripts, to mimic the desired behavior. I'm implementing the helper function in the integrations module first and will see how it behaves and take the learnings from that into this module. Stay tuned! |
And I finally had the time to implement the needed helper function. 0.5.40 is the release that you guys need to install! Running Enable-D365Exception as the VERY first cmdlet in any script / PowerShell console will enable exceptions. Not all cmdlets will throw exceptions from the beginning. Let me know when you hit one that should throw exceptions. Better yet, look at those that does it and help me implement the feature in the ones that you believe should be exception enabled. |
@Splaxi
|
Heh.. How to disable it now... UPDATE: Disable is $PSDefaultParameterValues['*:EnableException'] = $false |
Thx for the feedback. If you start a new powershell session / console, or simply I did a quick fix and push, give it 5-10 minutes and then make sure to update to version 0.5.41 |
And it is released... |
I run Switch-D365ActiveDatabase on the environment that already has the AxDB_original database. Switch-D365ActiveDatabase just give me a warning message that I need to remove
AxDB_original database.
I think it shouldn't be a warning. It should an Error message. Because database switching has been failed. And I should re-execute the Switch-D365ActiveDatabase after error fixing
The text was updated successfully, but these errors were encountered: