Error Handling & Debugging Profesional
SeriLevel 5 — Produksi & Lanjutan1 / 4

Error Handling & Debugging Profesional

Tangani error dengan try-catch, log terstruktur, dan cara debug script agar tidak berhenti mendadak.

try-catch#

Bungkus kode yang mungkin gagal dengan try-catch agar script tidak berhenti total.

Example:

function proses() {
try {
const data = ambilDariApi();
tulisKeSheets(data);
} catch (err) {
Logger.log('Gagal proses: ' + err.message);
GmailApp.sendEmail(Session.getActiveUser().getEmail(),
'Script error', err.stack);
}
}

Stack Trace#

err.stack berisi baris penyebab error. Berguna untuk debug.

Example:

catch (err) {
Logger.log(err.stack); // menunjukkan urutan pemanggilan
}

Log Terstruktur#

Gunakan objek agar log mudah dibaca.

Example:

function logStep(step, data) {
Logger.log(JSON.stringify({ step: step, waktu: new Date(), data: data }));
}

Mode Debug#

Validasi Input#

Example:

function olahBaris(r) {
if (!r || r.length < 2) {
Logger.log('Baris tidak valid, dilewati');
return;
}
// lanjut proses
}

Stackdriver / Cloud Logging#

Untuk script terhubung ke Cloud Project, console.log muncul di Google Cloud Logging (bisa filter, periksa histori lama).

TeknikKegunaan
try-catchCegah berhenti total
err.stackLacak sumber error
BreakpointDebug per baris
ExecutionsRiwayat jalannya fungsi
Cloud LoggingLog jangka panjang

Warning

Jangan biarkan catch kosong (catch(e){}). Error akan tersembunyi dan sulit dilacak.

Next#

Pelajari Library & Modularisasi.

Lisensi

CC BY-NC-SA 4.0 Karya ini dilisensikan di bawah Lisensi Creative Commons Atribusi-NonKomersial-BerbagiSerupa 4.0 Internasional.

Butuh jasa automatisasi atau custom Apps Script?

Jangan ragu untuk berkonsultasi. Kami bantu bangun solusi sesuai kebutuhan Anda.

Konsultasi sekarang →