Do you guys have an examples of creating a custom ...
# dev-metaflow
c
Do you guys have an examples of creating a custom decorator for each step?
1
I am trying to make a decorator that notifies slack...
Copy code
def notify_slack_on_error(current):
    def decorator(func):
        def wrapped(*args, **kwargs) :
            try:
                return func(*args, **kwargs)
            except Exception as e: 
                slack = Slack(flow_name=current.flow_name)
                slack.log_error(message=f"Step: {current.step_name} failed with error: {e}")
        
        return wrapped
    return decorator
but when I apply it I get an error
Step start specifies a self.next() transition to an unknown step, wrapped.
s
here's an example that does pretty much what you want - just replace Sentry with Slack: https://outerbounds-community.slack.com/archives/C020U025QJK/p1631684845032200?thread_ts=1631680543.031900&cid=C020U025QJK
c
Unbelievable, thank you so much @victorious-lawyer-58417
🙌 1
Hey @victorious-lawyer-58417, I implemented it and it works great with an argument. For your future reference when you need to help people out, this is what it looks like:
Copy code
def notify_slack_on_error(current):
    def decorator(func):
        @wraps(func)
        def wrapped(self) :
            try:
                return func(self)
            except Exception as e: 
                slack = Slack(flow_name=current.flow_name)
                slack.log_error(message=f"Step: {current.step_name} failed with error: {e}")
                raise
        
        return wrapped
    return decorator
v
Great! Surfacing this for the whole channel: See this thread for a custom decorator that sends step errors to Slack S thanks @careful-dress-39510!
🙌 2