Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. Blockly
    5. HTTP-Post und Headerübergabe

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    HTTP-Post und Headerübergabe

    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      HackyPaddy @haus-automatisierung last edited by

      @haus-automatisierung

      Bekommt man die .catch-Funktion denn im Blockly irgendwie dargestellt?

      haus-automatisierung 1 Reply Last reply Reply Quote 0
      • haus-automatisierung
        haus-automatisierung Developer Most Active @HackyPaddy last edited by

        @hackypaddy Aktuell nicht. Das müsste man direkt in der JS-Funktion lösen und sich überlegen was man dann zurückliefert.

        H 1 Reply Last reply Reply Quote 0
        • H
          HackyPaddy @haus-automatisierung last edited by

          @haus-automatisierung

          OK. Theoretisch müsste nichts zurück geliefert werden. Am besten wäre ein Eintrag ins Log und dann bricht das Script ab.

          Wird eh alle 10 Minuten aufgerufen...

          Nur der Adapter sollte nicht mehr abschmieren, das ist mehr als ungünstig 😞

          Ich habe ja dein Script von oben genauso eingesetzt. Kann man das einfach auf try und catch umbauen?

          z.B. so:

          try {
          return httpPostAsync(
              'http://jsonplaceholder.typicode.com/posts',
              {
                  title: 'foo',
                  body: 'bar',
                  userId: 1
              },
              {
                  timeout: 2000,
                  headers: {
                      'Content-Type': 'application/json'
                  }
              }
          )
          }
          catch {
              console.warn("Test123");
          }
          
          
          haus-automatisierung 1 Reply Last reply Reply Quote 0
          • haus-automatisierung
            haus-automatisierung Developer Most Active @HackyPaddy last edited by

            @hackypaddy sagte in HTTP-Post und Headerübergabe:

            Kann man das einfach auf try und catch umbauen?

            Theoretisch ja, aber dann geben manche Pfade ja keinen Wert zurück. Also könntest Du wieder Fehler bekommen, wenn Du in Blockly dann auf die Attribute zugreifen möchtest (weil ja nichts zurückgegeben wird). Da könnte man dann ein neues Objekt mit Dummy-Werten zurückliefern.

            z.B.

            try {
                return httpPostAsync(
                    'http://jsonplaceholder.typicode.com/posts',
                    {
                        title: 'foo',
                        body: 'bar',
                        userId: 1
                    },
                    {
                        timeout: 2000,
                        headers: {
                            'Content-Type': 'application/json'
                        }
                    }
                );
            }
            catch (err) {
                console.warn(err);
            
                return {
                    statusCode: -1,
                    data: '',
                    headers: [],
                };
            }
            
            H 1 Reply Last reply Reply Quote 0
            • H
              HackyPaddy @haus-automatisierung last edited by

              @haus-automatisierung

              Perfekt, so könnte ich über den Statuscode im Blockly weiter machen. Das ne gute Idee!

              Sagmal, bietest Du auch einen MasterKurs "JavaScript Advance" an. Wäre mal ne Idee 😁

              haus-automatisierung 1 Reply Last reply Reply Quote 0
              • haus-automatisierung
                haus-automatisierung Developer Most Active @HackyPaddy last edited by

                @hackypaddy sagte in HTTP-Post und Headerübergabe:

                Sagmal, bietest Du auch einen MasterKurs "JavaScript Advance" an.

                Hatte ich mir überlegt, aber wo fängt man da an? All das hier sind ja JavaScript-Grundlagen und haben erstmal nichts mit dem ioBroker zu tun.

                Und wenn ich ganz vorne anfange, dann ist es ein extrem weiter Weg der ganzen Grundlagen, bis ich überhaupt mal bei ioBroker-Themen ankomme. Und dann sind wir so weit vom Smart Home Thema weg, dass ich mich erstmal dagegen entschieden habe.

                H 1 Reply Last reply Reply Quote 0
                • H
                  HackyPaddy @haus-automatisierung last edited by

                  @haus-automatisierung

                  Also ich wäre dabei. Ich muss eh alles von Grund neu lernen, bin schon viel zu lange raus.

                  Aber! Die Lösung mit try catch läuft auch nicht. Ich hab testweise mal die URL geändert, sodass es zwangsläufig zum Fehler kommt. Er steigt wieder aus mit dem Hinweis, kein .catch oder Catch-Block vorhanden. Der Adapter schmiert ab...

                  
                  javascript.0
                  2024-11-05 11:30:00.205	error	unhandled promise rejection: getaddrinfo ENOTFOUND XXX
                  
                  javascript.0
                  2024-11-05 11:30:00.205	error	Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
                  

                  Hmm, hast noch ne Idee?

                  haus-automatisierung 1 Reply Last reply Reply Quote 0
                  • haus-automatisierung
                    haus-automatisierung Developer Most Active @HackyPaddy last edited by

                    @hackypaddy sagte in HTTP-Post und Headerübergabe:

                    Aber! Die Lösung mit try catch läuft auch nicht.

                    Ach sorry, wenn man nebenbei etwas anderes macht, ... Das liegt daran, dass ja das Promise zurückgegeben wird und es außerhalb ausgeführt wird. So sollte es klappen:

                    try {
                        const response = await httpPostAsync(
                            'http://jsonplaceholder.typicode.com/posts',
                            {
                                title: 'foo',
                                body: 'bar',
                                userId: 1
                            },
                            {
                                timeout: 2000,
                                headers: {
                                    'Content-Type': 'application/json'
                                }
                            }
                        );
                    
                        return response;
                    } catch (err) {
                        console.warn(err);
                     
                        return {
                            statusCode: -1,
                            data: '',
                            headers: [],
                        };
                    }
                    
                    H 1 Reply Last reply Reply Quote 0
                    • H
                      HackyPaddy @haus-automatisierung last edited by

                      @haus-automatisierung

                      Diesmal klappt es.
                      Genau da brauch ich Nachhilfe 😂

                      Er gibt jetzt den Statuscode vom Catch Block zurück und der Adapter schmiert nicht mehr ab.

                      Kann man den JavaScript-Adapter die Errormeldung im Log noch irgendwie abgewöhnen?

                      javascript.0
                      2024-11-05 11:58:00.018	error	script.js.common.ATU.Messdaten_senden: httpPost(url=XXX, error=getaddrinfo ENOTFOUND XXX)
                      

                      Ich hab ja schon die Behandlung im Blockly, wäre doppelt gemoppelt.

                      haus-automatisierung 1 Reply Last reply Reply Quote 0
                      • haus-automatisierung
                        haus-automatisierung Developer Most Active @HackyPaddy last edited by

                        @hackypaddy sagte in HTTP-Post und Headerübergabe:

                        Kann man den JavaScript-Adapter die Errormeldung im Log noch irgendwie abgewöhnen?

                        Gibt dazu schon einen Feature Request auf GitHub. Muss halt jemand machen 🙂

                        H 1 Reply Last reply Reply Quote 0
                        • H
                          HackyPaddy @haus-automatisierung last edited by

                          @haus-automatisierung

                          Ok, dann halte ich die Füße still 😀

                          Danke Dir für die Hilfe!

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post

                          Support us

                          ioBroker
                          Community Adapters
                          Donate

                          484
                          Online

                          31.9k
                          Users

                          80.1k
                          Topics

                          1.3m
                          Posts

                          3
                          25
                          1214
                          Loading More Posts
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes
                          Reply
                          • Reply as topic
                          Log in to reply
                          Community
                          Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                          The ioBroker Community 2014-2023
                          logo