How to improve my code where I need to undo what I did #1198
-
Here is the code : public EitherAsync<IError, StatusMonitor> Execute(User user, OttdServer server, ulong guildId, ulong channelId)
{
var embedMessageIdResult = CreateEmbedMessage(channelId, server);
return
(
from _1 in CheckIfHasCorrectUserLevel(user, UserLevel.Admin).ToAsync()
from messageId in embedMessageIdResult
from statusMonitor in statusMonitorRepository.Insert(new StatusMonitor(
server.Id,
guildId,
channelId,
messageId,
DateTime.MinValue.ToUniversalTime()))
from _2 in InformActor(statusMonitor)
select statusMonitor)
.MapLeft(err =>
{
if (embedMessageIdResult.IsRight.Result)
{
DeleteEmbedMessage(channelId, embedMessageIdResult.Right());
}
return err;
});
} I am new to this library and I am wondering if there is any nicer approach to handle errors in this case to call |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Very interesting question. I'm interested in nice solutions for this, too. Finally this is similar to database transactions where you can rollback (or commit). IMHO code gets more functional (FP style) if we try to avoid any intermediate side effect. But I find this is quite often impossible. An easy example is "create 2 files" which might fail after file 1 is created. Maybe something like this:
Note: edited because code was not working / improved test showing both cases. |
Beta Was this translation helpful? Give feedback.
Very interesting question. I'm interested in nice solutions for this, too.
Finally this is similar to database transactions where you can rollback (or commit). IMHO code gets more functional (FP style) if we try to avoid any intermediate side effect. But I find this is quite often impossible. An easy example is "create 2 files" which might fail after file 1 is created.
Maybe something like this: