Repository: louislam/uptime-kuma
File: server/model/monitor.js (JavaScript)

Code before:
                    } else if (this.dns_resolve_type == 'MX') {
                        dnsRes.forEach(record => {
                            dnsMessage += `Server: ${record.exchange} - Priority: ${record.priority} | `;
                        });
                        var dnsMessage = dnsMessage.slice(0, -2)
                    } else if (this.dns_resolve_type == 'NS') { 
                        dnsRes.forEach(record => {
                            dnsMessage += `Server: ${record} | `;
                        });
                        var dnsMessage = dnsMessage.slice(0, -2)
                    } else if (this.dns_resolve_type == 'SOA') {
                        dnsMessage += `NS-Name: ${dnsRes.nsname} | Hostmaster: ${dnsRes.hostmaster} | Serial: ${dnsRes.serial} | Refresh: ${dnsRes.refresh} | Retry: ${dnsRes.retry} | Expire: ${dnsRes.expire} | MinTTL: ${dnsRes.minttl}`;
                    } else if (this.dns_resolve_type == 'SRV') {
                        dnsRes.forEach(record => {
                            dnsMessage += `Name: ${record.name} | Port: ${record.port} | Priority: ${record.priority} | Weight: ${record.weight} | `;
                        });
                        var dnsMessage = dnsMessage.slice(0, -2)
                    } else if (this.dns_resolve_type == 'TXT') {
                        dnsRes.forEach(record => {
                            dnsMessage += `Record: ${record} | `;
                        });
                        var dnsMessage = dnsMessage.slice(0, -2)
                    }

                    bean.msg = dnsMessage;
                    bean.ping = dayjs().valueOf() - startTime;
                    bean.status = UP;
                }

                if (this.isUpsideDown()) {
                    bean.status = flipStatus(bean.status);

                    if (bean.status === DOWN) {
                        throw new Error("Flip UP to DOWN");
                    }
                }

                retries = 0;

            } catch (error) {

                bean.msg = error.message;

                // If UP come in here, it must be upside down mode
                // Just reset the retries
                if (this.isUpsideDown() && bean.status === UP) {
                    retries = 0;

                } else if ((this.maxretries > 0) && (retries < this.maxretries)) {
                    retries++;
                    bean.status = PENDING;

Review comment (suggestion):
Suggest moving `bean.ping = ...` to line 184 to increase timing accuracy.

Also please fix eslint errors by using let instead of var and remove re-declaration.

Code after:
                    } else if (this.dns_resolve_type == "CAA") {
                        dnsMessage = dnsRes[0].issue;
                    } else if (this.dns_resolve_type == "MX") {
                        dnsRes.forEach(record => {
                            dnsMessage += `Hostname: ${record.exchange} - Priority: ${record.priority} | `;
                        });
                        dnsMessage = dnsMessage.slice(0, -2)
                    } else if (this.dns_resolve_type == "NS") {
                        dnsMessage += "Servers: ";
                        dnsMessage += dnsRes.join(" | ");
                    } else if (this.dns_resolve_type == "SOA") {
                        dnsMessage += `NS-Name: ${dnsRes.nsname} | Hostmaster: ${dnsRes.hostmaster} | Serial: ${dnsRes.serial} | Refresh: ${dnsRes.refresh} | Retry: ${dnsRes.retry} | Expire: ${dnsRes.expire} | MinTTL: ${dnsRes.minttl}`;
                    } else if (this.dns_resolve_type == "SRV") {
                        dnsRes.forEach(record => {
                            dnsMessage += `Name: ${record.name} | Port: ${record.port} | Priority: ${record.priority} | Weight: ${record.weight} | `;
                        });
                        dnsMessage = dnsMessage.slice(0, -2)
                    }

                    bean.msg = dnsMessage;
                    bean.status = UP;
                }

                if (this.isUpsideDown()) {
                    bean.status = flipStatus(bean.status);

                    if (bean.status === DOWN) {
                        throw new Error("Flip UP to DOWN");
                    }
                }

                retries = 0;

            } catch (error) {

                bean.msg = error.message;

                // If UP come in here, it must be upside down mode
                // Just reset the retries
                if (this.isUpsideDown() && bean.status === UP) {
                    retries = 0;

                } else if ((this.maxretries > 0) && (retries < this.maxretries)) {
                    retries++;
                    bean.status = PENDING;
                }
            }

            // * ? -> ANY STATUS = important [isFirstBeat]
            // UP -> PENDING = not important
            // * UP -> DOWN = important